Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The calling order of event handlers is non-deterministic #99

Closed
z-rui opened this issue Sep 12, 2016 · 4 comments
Closed

The calling order of event handlers is non-deterministic #99

z-rui opened this issue Sep 12, 2016 · 4 comments

Comments

@z-rui
Copy link

z-rui commented Sep 12, 2016

It can be seen that (in events.go) the event handlers are called in a separate goroutine. Therefore, there is no guarantee of the order of the handlers being called.

This is especially harmful to keyboard events. Consider this situation: some characters are pasted to the terminal and so a bunch of keyboard events are generated in a short time, but the order of the corresponding handlers being called are kind of random. If the program is a text editor, the actual pasted text will be shuffled.

To fix the problem, event handlers should be called synchrously. A separate goroutine can always be created in the event handler if needed.

PS: The order of keyboard events seemed to be preserved after I changed two go statements (in evtKbd() and (*EvtStream) Loop()) to simple synchrounous function calls.

@wanzysky
Copy link
Contributor

+1

@hasryan
Copy link

hasryan commented Jan 28, 2017

@z-rui can you share the code changes you made? I am trying to accept strings pasted from the clipboard but the characters arrived in scrambled order, and I suspect it is caused by the issue you've described here.

@z-rui
Copy link
Author

z-rui commented Jan 28, 2017

Last time I tried:

diff --git a/events.go b/events.go
index 177bbb4..3ff7fdd 100644
--- a/events.go
+++ b/events.go
@@ -126,7 +126,7 @@ func hookTermboxEvt() {
                e := termbox.PollEvent()

                for _, c := range sysEvtChs {
-                       go func(ch chan Event) {
+                       func(ch chan Event) {
                                ch <- crtTermboxEvt(e)
                        }(c)
                }
@@ -236,7 +236,7 @@ func (es *EvtStream) Loop() {
                case "/sig/stoploop":
                        return
                }
-               go func(a Event) {
+               func(a Event) {
                        es.RLock()
                        defer es.RUnlock()
                        if pattern := es.match(a.Path); pattern != "" {

@hasryan
Copy link

hasryan commented Jan 29, 2017

Thank you @z-rui your patch fixed the problem for me.

@gizak gizak mentioned this issue Mar 6, 2017
17 tasks
@gizak gizak closed this as completed in 86c275c Apr 6, 2017
kamisdev pushed a commit to kamisdev/powerfulTERMINAL that referenced this issue May 7, 2022
sadevn added a commit to sadevn/go-terminal that referenced this issue Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants