Skip to content

Commit 5534379

Browse files
committed
Fix connection leak, tighten socket-dir assertion, fix Windows start
- assert_executor_start_stop: capture conn returned by retry() and call conn.close() so the readiness backend session is released promptly instead of waiting for GC. - test_executor_with_special_chars_in_all_paths: strengthen the Unix unix_socket_directories assertion to include the actual socket_dir path value, so a missing or mis-quoted path fails the test. - PostgreSQLExecutor.start(): on Windows, run pg_ctl start -w synchronously via shell=True rather than delegating to mirakuru's subprocess polling. pg_ctl exits as soon as the server is ready, so mirakuru's check_subprocess loop always sees a dead launcher process and times out; bypassing it removes the 60-second timeout failure seen in test_noproc_cached_version and related Windows CI jobs. Made-with: Cursor
1 parent 60fa1b9 commit 5534379

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

pytest_postgresql/executor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ def start(self: T) -> T:
176176
f"The currently installed version of PostgreSQL: {self.version}."
177177
)
178178
self.init_directory()
179+
if platform.system() == "Windows":
180+
# On Windows, pg_ctl start -w exits as soon as the server is
181+
# accepting connections. mirakuru's polling loop calls
182+
# check_subprocess() (our pg_ctl-status override) repeatedly
183+
# while waiting for the launcher subprocess to indicate readiness,
184+
# but by the time polling begins the launcher has already exited
185+
# so the loop never sees a live process and times out.
186+
# Running the command synchronously via the shell lets cmd.exe
187+
# handle quoting and lets pg_ctl itself act as the readiness
188+
# barrier, which is more reliable than mirakuru's approach here.
189+
result = subprocess.run(self.command, shell=True, check=False)
190+
if result.returncode != 0:
191+
raise ProcessFinishedWithError(self, result.returncode)
192+
return self
179193
return super().start()
180194

181195
def clean_directory(self) -> None:

tests/test_executor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def assert_executor_start_stop(executor: PostgreSQLExecutor) -> None:
2727
# becomes accessible before PostgreSQL finishes database recovery,
2828
# so an immediate connect may raise OperationalError with
2929
# "the database system is starting up".
30-
retry(
30+
conn = retry(
3131
lambda: psycopg.connect(
3232
dbname=executor.user,
3333
user=executor.user,
@@ -37,6 +37,7 @@ def assert_executor_start_stop(executor: PostgreSQLExecutor) -> None:
3737
),
3838
possible_exception=psycopg.OperationalError,
3939
)
40+
conn.close()
4041
with pytest.raises(psycopg.OperationalError):
4142
psycopg.connect(
4243
dbname=executor.user,
@@ -233,7 +234,7 @@ def test_executor_with_special_chars_in_all_paths(
233234
if current_platform == "Windows":
234235
assert "unix_socket_directories" not in executor.command
235236
else:
236-
assert "unix_socket_directories='" in executor.command
237+
assert f"unix_socket_directories='{socket_dir}'" in executor.command
237238

238239
# Start and stop the executor to verify it works
239240
assert_executor_start_stop(executor)

0 commit comments

Comments
 (0)