From 29c7555eee5e2aab06589ca924a4653d6e51bbcf Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 20 May 2025 15:30:17 -0700 Subject: [PATCH 1/3] add option for launching in own process group --- config.go | 7 +++++++ embedded_postgres.go | 2 ++ execopts_unix.go | 18 ++++++++++++++++++ execopts_windows.go | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 execopts_unix.go create mode 100644 execopts_windows.go diff --git a/config.go b/config.go index 35e9a98..a55766c 100644 --- a/config.go +++ b/config.go @@ -24,6 +24,7 @@ type Config struct { binaryRepositoryURL string startTimeout time.Duration logger io.Writer + ownProcessGroup bool } // DefaultConfig provides a default set of configuration to be used "as is" or modified using the provided builders. @@ -144,6 +145,12 @@ func (c Config) BinaryRepositoryURL(binaryRepositoryURL string) Config { return c } +// OwnProcessGroup configures whether the server should be started in its own process group. +func (c Config) OwnProcessGroup(ownProcessGroup bool) Config { + c.ownProcessGroup = ownProcessGroup + return c +} + func (c Config) GetConnectionURL() string { return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", c.username, c.password, "localhost", c.port, c.database) } diff --git a/embedded_postgres.go b/embedded_postgres.go index afe8497..de09423 100644 --- a/embedded_postgres.go +++ b/embedded_postgres.go @@ -216,6 +216,7 @@ func startPostgres(ep *EmbeddedPostgres) error { "-o", encodeOptions(ep.config.port, ep.config.startParameters)) postgresProcess.Stdout = ep.syncedLogger.file postgresProcess.Stderr = ep.syncedLogger.file + applyPlatformSpecificOptions(postgresProcess, ep.config) if err := postgresProcess.Run(); err != nil { _ = ep.syncedLogger.flush() @@ -233,6 +234,7 @@ func stopPostgres(ep *EmbeddedPostgres) error { "-D", ep.config.dataPath) postgresProcess.Stderr = ep.syncedLogger.file postgresProcess.Stdout = ep.syncedLogger.file + applyPlatformSpecificOptions(postgresProcess, ep.config) if err := postgresProcess.Run(); err != nil { return err diff --git a/execopts_unix.go b/execopts_unix.go new file mode 100644 index 0000000..90f9d7e --- /dev/null +++ b/execopts_unix.go @@ -0,0 +1,18 @@ +//go:build !windows +// +build !windows + +package embeddedpostgres + +import ( + "os/exec" + "syscall" +) + +func applyPlatformSpecificOptions(cmd *exec.Cmd, config Config) { + if config.ownProcessGroup { + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.Setpgid = true + } +} diff --git a/execopts_windows.go b/execopts_windows.go new file mode 100644 index 0000000..cdeb0f7 --- /dev/null +++ b/execopts_windows.go @@ -0,0 +1,18 @@ +//go:build windows +// +build windows + +package embeddedpostgres + +import ( + "os/exec" + "syscall" +) + +func applyPlatformSpecificOptions(cmd *exec.Cmd, config Config) { + if config.ownProcessGroup { + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.CreationFlags = syscall.CREATE_NEW_PROCESS_GROUP + } +} From 7661ebea7c548479bd11179922b87dd3dc9ee99d Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 20 May 2025 16:26:24 -0700 Subject: [PATCH 2/3] use in test --- embedded_postgres_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/embedded_postgres_test.go b/embedded_postgres_test.go index e7e98b3..393ffe3 100644 --- a/embedded_postgres_test.go +++ b/embedded_postgres_test.go @@ -258,6 +258,7 @@ func Test_CustomConfig(t *testing.T) { StartTimeout(10 * time.Second). Locale("C"). Encoding("UTF8"). + OwnProcessGroup(true). Logger(nil)) if err := database.Start(); err != nil { From b0ca281de480d3e531e8585f5601fb6a1da7f8f4 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 20 May 2025 16:38:57 -0700 Subject: [PATCH 3/3] .