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

Call pselect twice to not starve signal handling (alternative to #130) #132

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

hartwork
Copy link
Collaborator

Alternative to #130.

With both aggressive stdin input and signals coming in, pselect sometimes delays signal delivery for multiple seconds, to the point where even holding Ctrl+C down for seconds will not be successful in terminating the process. I have witnessed this symptom for a duration of 20+ seconds in practice.

To reproduce:

# ./ttyplot < /dev/zero
[hold Ctrl+C or press repeatedly]
[see it take multiple seconds for the process to terminate (if at all)]

Related:
https://stackoverflow.com/questions/62315082/pselect-on-linux-does-not-deliver-signals-if-events-are-pending

@hartwork hartwork requested a review from edgar-bonet November 28, 2023 15:53
@hartwork hartwork force-pushed the call-pselect-twice branch 4 times, most recently from ef364b5 to b621831 Compare November 28, 2023 16:56
@edgar-bonet
Copy link
Collaborator

This has a few issues:

@hartwork
Copy link
Collaborator Author

hartwork commented Nov 28, 2023

@edgar-bonet I understand that this is one of the more controversial among the pull requests we had so far, and we don't have to rush this.

Regarding your points:

Personally I would rather rebase #115 on top of this after #132 has been merged; the conflict resolution should be trivial, if it's not I would volunteer for the resolution.

  • It has a race condition for signals delivered between the two pselect calls.

Yes, a race condition that will cause up to ~200ms delay in handling of Ctrl+C compared to 20+ seconds on current development.

  • It makes the program repaint way more often than needed, even in the most common case of reasonable stdin pressure.

Yes, but so far I considered rendering at 5 fps acceptable, we can discus that or revert back to 500ms and be conscious about it.

To summarize, at the moment all these combined seem minor to me when compared to non-functioning Ctrl+C.

@hartwork
Copy link
Collaborator Author

@edgar-bonet I have dropped the increase in painting rate now, it only now only adds the second call to pselect. That will help keeping Ctrl+C working even with high pressure and the race condition will cause a few milliseconds in delay at worst and not do any other damage. I believe this is ready to go, thanks for your feedback and consideration.

@hartwork
Copy link
Collaborator Author

@edgar-bonet I have extracted a function pselect_without_signal_starvation now to restore the feel that there is one single event waiting location in the code. I hope you like it.

@hartwork hartwork force-pushed the call-pselect-twice branch 2 times, most recently from 04f7fed to 30622f8 Compare November 29, 2023 19:25
Copy link
Collaborator

@edgar-bonet edgar-bonet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still has a race condition. But the window of vulnerability is small, so this will do for now. Will probably implement a race-free alternative another day.

@hartwork
Copy link
Collaborator Author

This still has a race condition. But the window of vulnerability is small, so this will do for now. Will probably implement a race-free alternative another day.

@edgar-bonet perfect, thank you! 🙏

With both aggressive stdin input and signals coming in,
`pselect` sometimes delays signal delivery for multiple
seconds, to the point where even holding Ctrl+C down
for seconds will not be successful in terminating
the process.  I have witnessed this symptom for a
duration of 20+ seconds in practice.

To reproduce:
> # ./ttyplot < /dev/zero
> [hold Ctrl+C or press repeatedly]
> [see it take multiple seconds for the process to terminate (if at all)]

Related:
https://stackoverflow.com/questions/62315082/pselect-on-linux-does-not-deliver-signals-if-events-are-pending
@hartwork
Copy link
Collaborator Author

@edgar-bonet rebasing onto latest development for a green CI…

@hartwork hartwork merged commit 6869c8b into tenox7:development Nov 29, 2023
7 checks passed
@hartwork hartwork added this to the 1.6.0 milestone Nov 29, 2023
@hartwork hartwork deleted the call-pselect-twice branch November 29, 2023 21:52
edgar-bonet added a commit that referenced this pull request Dec 3, 2023
Catch signals using a self-pipe (follow-up to #98 and #132)
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

Successfully merging this pull request may close these issues.

2 participants