Skip to content

Conversation

mattjohnsonpint
Copy link

In order to handle shutdown cleanly in an app, it's important that the application starting the db instance with Start is also the one the calls Stop to shut it down. In the case where an app is running until the user terminates it with a Ctrl-C, the app needs to be able to trap the SIGINT and then call Stop.

Unfortunately, there's some platform differences in how Ctrl-C is propagated to child processes between platforms. On Linux/Mac, the interrupt request is passed only to the parent process, but on Windows it is passed to ALL child processes in the same process group as well. So by the time the parent app goes to call Stop - the server is already terminated.

This PR adds a config flag, OwnProcessGroup which when passed true will make sure that the exec commands that call pg_ctl (and in turn, the postgres server itself) are launched in their own process group. This will change the Ctrl-C behavior on Windows to behave the same as on Linux/Mac.

For good measure, I also set the appropriate flag on Linux/Mac so the behavior is truly consistent in all regards. It's SysProcAttr.Setpgid = true on Linux/Mac, and SysProcAttr.CreationFlags = syscall.CREATE_NEW_PROCESS_GROUP on Windows.

@fergusstrange
Copy link
Owner

Apologies for the time taken to review this one.

Do you think it's worth defaulting this behaviour rather than adding as config @mattjohnsonpint? I can't see a reason we wouldn't always want this.

@mattjohnsonpint
Copy link
Author

Yeah, that would make sense to me.

@mattjohnsonpint
Copy link
Author

It's been a while since I made this, but IIRC I was thinking that there must be some use case for the current behavior, and I didn't want to add such a drastic change without it being an option - in case it messed things up for others.

@fergusstrange
Copy link
Owner

I don't recall a specific reason for setting it up this way! If you're happy to switch it around and the tests pass let's do it 👍

@mattjohnsonpint
Copy link
Author

Cool. I'll send a revision soon. 👍

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