fix: ensure proper process termination when using --exit-no-conn or --once #1462
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.
Problem
Resolve #1461
When using
--exit-no-connor--onceoptions, ttyd would exit immediately without properly waiting for the child process to terminate. This could result in:Root Cause
On certain systems (particularly macOS), the
waitpid()system call may fail to properly reap zombie processes in some edge cases, causing theprocess_exit_cbcallback to never be invoked, leading ttyd to wait indefinitely.Solution
Implemented a dual-mechanism approach for robust process termination handling:
waitpid()andprocess_exit_cblogicuv_timer_tthat periodically checks process status usingkill(pid, 0)If the primary mechanism fails, the fallback timer detects when the process no longer exists and triggers ttyd exit.
Changes
exit_timerfield topty_ctx_tstructureexit_timer_cb()for periodic process status checking--onceand--exit-no-connbehavior to properly wait for process terminationTesting
asciinema--onceor--exit-no-connCompatibility
--onceor--exit-no-connoptionsFixes the issue where ttyd would not properly wait for child process termination, ensuring reliable signal delivery and process cleanup.