Fix race conditions as mentioned in #148 #169
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR resolves the race conditions mentioned in #148.
The race condition was causing rendering glitches with prompts for me, it annoyed me sufficiently that I decided to investigate why the rendering was glitching and wrote a fix. Here's a PR for it if you'd like.
In summary what happens in most cases is when you hit "enter" in a prompt, an "ioloop" goroutine in the readline package dispatches the event to both the listen handler, and to the code that returns from
.Readline()
simultaneously. Because both the listen handler and the code that occurs after.Readline()
returns in promptui mutate the readline screen buffer without synchronization, a data race occurs, and can cause rendering glitches depending on the order of execution (not to mention the data race itself is unsafe anyway).This fix adds a mutex to synchronize the access between the listen handler and the after
.Readline()
return handling, additionally a closing variable is used to indicate to the listen handler when it should stop processing events as the prompt is quitting as to prevent the incorrect final text from being displayed.I've tested this code on the example provided in #148, and with my own code, and it appears to be functioning great now without any rendering glitches in prompts, nor any warnings from Go's race detector when running with
-race
.Reminder that after merging and testing, you'll probably want to release this as an update.