Skip to content

Conversation

@asonawalla
Copy link
Contributor

Summary

  • Remove os.Exit() calls from TestMain functions to allow deferred Stop() calls to execute properly
  • Fixes embedded postgres process leaks after test runs

Problem

The TestMain functions in 4 test files were calling os.Exit(code) after m.Run(). In Go, os.Exit() terminates the program immediately without running deferred functions. This meant defer sharedEmbeddedPG.Stop() never executed, leaving orphaned postgres processes after each test package run.

Solution

Go 1.15+ automatically handles the exit code when TestMain returns without calling os.Exit(). By simply removing the os.Exit() calls, the deferred cleanup now executes properly.

Ref: golang/go#34129

Test plan

  • Verified no postgres processes running before tests (ps aux | grep postgres)
  • Ran go test ./...
  • Confirmed postgres processes are cleaned up after tests complete
  • go build ./... passes

Remove os.Exit() calls from TestMain functions to allow deferred
Stop() calls to execute. In Go, os.Exit() terminates immediately
without running deferred functions, causing embedded postgres
processes to leak after test runs.

Go 1.15+ automatically handles the exit code when TestMain returns,
making explicit os.Exit() unnecessary.

Ref: golang/go#34129
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a resource leak issue where embedded PostgreSQL processes were not being cleaned up after test runs. The fix removes os.Exit() calls from TestMain functions in 4 test files, allowing deferred Stop() calls to execute properly. Since Go 1.15+, the testing framework automatically handles exit codes when TestMain returns without calling os.Exit(), making this change both safe and correct.

Changes:

  • Removed os.Exit() calls and simplified TestMain to just call m.Run() in 4 test files
  • Allows deferred cleanup functions (defer sharedEmbeddedPG.Stop()) to execute properly
  • Prevents orphaned PostgreSQL processes after test package runs

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
internal/plan/plan_test.go Removed os.Exit() to allow deferred postgres cleanup in TestMain
internal/diff/diff_test.go Removed os.Exit() to allow deferred postgres cleanup in TestMain
cmd/migrate_integration_test.go Removed os.Exit() to allow deferred postgres cleanup in TestMain
cmd/apply/apply_integration_test.go Removed os.Exit() to allow deferred postgres cleanup in TestMain

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

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

LGTM.

@tianzhou tianzhou merged commit 643b515 into pgplex:main Jan 15, 2026
7 checks passed
alecthomas pushed a commit to alecthomas/pgschema that referenced this pull request Jan 26, 2026
Remove os.Exit() calls from TestMain functions to allow deferred
Stop() calls to execute. In Go, os.Exit() terminates immediately
without running deferred functions, causing embedded postgres
processes to leak after test runs.

Go 1.15+ automatically handles the exit code when TestMain returns,
making explicit os.Exit() unnecessary.

Ref: golang/go#34129
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