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

fix: Prefer os.Process.Signal to syscall.Kill #228

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

anrddh
Copy link
Contributor

@anrddh anrddh commented Aug 19, 2024

This protects us from a potential (but rare) race condition where

  1. the process we are trying to signal terminates of its own accord
  2. its PID gets reaped in the os.Process.Wait call
  3. a new process with the same PID gets spawned

before our signal actually gets sent.

This is achieved by using the higher-level os.Process API directly, which
has some synchronization to prevent os.Process.Wait and os.Process.Signal
from racing.

This protects us from a potential (but rare) race condition where

1. the process we are trying to signal terminates of its own accord
2. its PID gets reaped in the os.Process.Wait call
3. a new process with the same PID gets spawned

before our signal actually gets sent.

This is achieved by using the higher-level os.Process API directly, which
has some synchronization to prevent os.Process.Wait and os.Process.Signal
from racing.
@anrddh anrddh force-pushed the fix-potential-race-condition branch from d84bf09 to a1e532e Compare August 19, 2024 17:28
Copy link

Copy link
Owner

@F1bonacc1 F1bonacc1 left a comment

Choose a reason for hiding this comment

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

Looks great!
Thanks.
How did you manage to catch it?!? 😄

@anrddh
Copy link
Contributor Author

anrddh commented Aug 20, 2024

I've encountered lots of subtle bugs around fork, exec, kill, wait and friends before, so I usually read code that uses them very carefully :)

It's fairly hard to use those syscalls correctly. I've even found discussion for this sort of thing in the Go standard library:
golang/go@cea29c4
golang/go#9382
golang/go#13987

@F1bonacc1 F1bonacc1 merged commit 9292f9f into F1bonacc1:main Aug 20, 2024
3 checks passed
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