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

Race condition between spinner thread and write() #29

Closed
sebageek opened this issue Mar 6, 2019 · 2 comments · Fixed by #35
Closed

Race condition between spinner thread and write() #29

sebageek opened this issue Mar 6, 2019 · 2 comments · Fixed by #35

Comments

@sebageek
Copy link
Contributor

sebageek commented Mar 6, 2019

When I use write() a lot on a spinner it often happens to me that the written text is displayed after the spinner message, like this:

[...]
noot noot
⠋ spinning...noot noot
noot noot
[...]

I used the script below to recreate the problem. It takes some time (400-800 writes) with it, but happens to me far more often with real world applications.

import random
import time

import yaspin

with yaspin.yaspin() as sp:
    sp.text = 'spinning...'
    while True:
        sp.write('noot noot')
        time.sleep(0.051 + random.randint(0, 10) / 100)

I think this happens because of concurrent access to sys.stdout.write() by the spinning thread and the spinners write(). This should be solvable by using a threading.Lock() and guarding access to sys.stdout.write() with it. In a quick local hack-fix-test it worked for me by doing this in write() and _spin(), but there are a lot more functions that would need guarding, if done right.

@pavdmyt
Copy link
Owner

pavdmyt commented Mar 6, 2019

Hi @sebageek

Thanks for reporting the issue! I'll take a look at it closer soon.
Please feel free to open PR.

sebageek added a commit to sebageek/yaspin that referenced this issue Apr 18, 2019
We now guard access to sys.stdout with a threading.Lock(), so
write() and the spinner thread no longer mangle up their
ouptut.

Fixes pavdmyt#29
sebageek added a commit to sebageek/yaspin that referenced this issue May 10, 2019
We now guard access to sys.stdout with a threading.Lock(), so
write() and the spinner thread no longer mangle up their
ouptut.

Fixes pavdmyt#29
pavdmyt pushed a commit that referenced this issue May 12, 2019
yaspin v0.14.3; (fix #29) racecondition between write() and spinner
@pavdmyt
Copy link
Owner

pavdmyt commented May 12, 2019

FIxed by #35 in yaspin v0.14.3.

Thanks @sebageek for contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants