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

Possible race condition between Fini and PollEvent #460

Closed
squizzling opened this issue May 16, 2021 · 4 comments
Closed

Possible race condition between Fini and PollEvent #460

squizzling opened this issue May 16, 2021 · 4 comments

Comments

@squizzling
Copy link

Both cScreen and tScreen will eventually set their stopQ channels to nil in response to Fini. This update of the struct field is protected by a mutex, however the read of the field in PollEvent is not, which triggers a data race.

While the documentation for PollEvent does indicate it should be called from the main loop, it isn't clear about who is allowed to call Fini. However from the locks, it appears that Fini is intended to be callable from anywhere, thus making the underlying data shared rather than exclusive to the main loop.

As such the data access should be protected in PollEvent as well, even if PollEvent is only allowed to be called from a specific routine. Alternatively, Fini should be documented to only be callable from the main goroutine as well (this is an API break, however)

@gdamore
Copy link
Owner

gdamore commented May 16, 2021

Fair point. What I'll do is move the clearing of that to after waiting for the waitgroup.

@gdamore gdamore changed the title Possible race condition / documentation gap between Screen.Fini and Screen.PollEvent Possible race condition between Screen.Fini and Screen.PollEvent May 16, 2021
@gdamore
Copy link
Owner

gdamore commented May 16, 2021

PollEvent does not read from the stopQ in the tscreen implementation. It would be an error to call PollEvent when the application is not started. I will change Fini not to clear it, but I do need to set it during start().

@gdamore gdamore changed the title Possible race condition between Screen.Fini and Screen.PollEvent Possible race condition between Fini and PollEvent May 16, 2021
@gdamore
Copy link
Owner

gdamore commented May 16, 2021

(For anyone watching ... its pretty unlikely that you'll ever actually hit this as a real world bug.)

@squizzling
Copy link
Author

PollEvent does not read from the stopQ in the tscreen implementation.

My bad, I confused myself!

Thanks for the quick response.

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

2 participants