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

Use Job Objects and equivalent things on Mac/Linux #10

Open
egonelbre opened this issue Apr 15, 2023 · 14 comments
Open

Use Job Objects and equivalent things on Mac/Linux #10

egonelbre opened this issue Apr 15, 2023 · 14 comments

Comments

@egonelbre
Copy link
Member

egonelbre commented Apr 15, 2023

Currently when starting things with watchrun go run main.go and main.go contains a server, then watchrun doesn't automatically kill the subprocess. Which is not ideal. We can overcome this limitation by monitoring which subprocesses are created and then ensure they are killed as a whole.

On Windows there are Job Objects, which allow such process groups that can be killed as a whole. The current approach for killing processes in Windows is rather hacky https://github.com/loov/watchrun/blob/master/pgroup/kill_windows.go#L19.

I'm not sure what the best approach on Unix is, but may be monitoring things with ptrace and capturing the pids?

@egonelbre
Copy link
Member Author

@gedw99 note, I'm that approach, but it doesn't seem to be sufficient. (https://github.com/loov/watchrun/blob/master/pgroup/kill_windows.go#L13)

@gedw99
Copy link

gedw99 commented Apr 18, 2023

Can you say why and explain it ??

@egonelbre
Copy link
Member Author

The issue is with subprocesses starting processes themselves and not chaining to the process group properly, so when you try to kill the process group the children won't be shutdown. Similarly, killing by PID can be problematic, because at the point of trying to kill, some other process may have started with the same PID (although, it's very unlikely). Similarly sending kill signal is notoriously in-effective on Windows, because processes can disregard that message.

@gedw99
Copy link

gedw99 commented Jun 6, 2023

if you are the author of all the processes that watch run is trying to control you can handle those aspects - PIDS; signals

If your not then it's harder to make sure they respect what the watchrun system is "telling them to do".

Is that a fair statement ?

@egonelbre
Copy link
Member Author

Kind of. It's more of, the default process group / tree in Windows doesn't fully contain the children. i.e. it allows to detach subprocesses, so killing the tree doesn't kill them. But, yes, AFAIK, if they properly handle the process grouping themselves, then there should be less issues.

@gedw99
Copy link

gedw99 commented Jun 7, 2023

well i call this progress :)

We could make dummy services to refine those touchy areas - just to get the ball rolling and then also work through these things ?

Also the dummy services would show others how to respect the signalling watchrun needs.

@egonelbre
Copy link
Member Author

Well, that seems more work than getting job objects etc. similar working.

@gedw99
Copy link

gedw99 commented Jun 7, 2023

i guess i don't know the context of what Job Objects is meant to do etc ... could you explain or point me to it ?

@egonelbre
Copy link
Member Author

@gedw99
Copy link

gedw99 commented Jun 7, 2023

sure but the objective is monitoring ? give me the higher level reason for it.

@gedw99
Copy link

gedw99 commented Jun 7, 2023

don't know if this helps: https://github.com/kolesnikovae/go-winjob

@egonelbre
Copy link
Member Author

egonelbre commented Jun 7, 2023

The objective isn't monitoring, it's just to ensure that the process and all sub-processes get shutdown.

And, yes, I'm aware that there are packages that implement winjob api. For Windows, I'll probably use https://pkg.go.dev/golang.org/x/sys/windows directly. Watchrun needs a small set of features, so directly using winapi should be sufficient.

@gedw99
Copy link

gedw99 commented Jun 8, 2023

ok thats sounds wise too.

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

No branches or pull requests

2 participants