Skip to content

Commit f8d2fbe

Browse files
committed
gemini review
1 parent 28c36fa commit f8d2fbe

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

windows/db_backend.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import os
2727
import subprocess
2828
import tempfile
29+
import threading
2930
from urllib.parse import urlparse
3031

3132
from windows import paths
@@ -35,6 +36,7 @@
3536
_USE_PGSERVER = None
3637

3738
_NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0)
39+
_patch_lock = threading.Lock()
3840

3941

4042
def _check_pgserver():
@@ -183,22 +185,26 @@ def _patch_pgserver_pg_ctl():
183185
import pgserver.postgres_server as ps
184186
if getattr(ps, "_audiomuse_pg_ctl_patched", False):
185187
return
186-
original = ps.pg_ctl
187-
timeout = paths.pg_start_timeout()
188-
189-
def pg_ctl(args, **kwargs):
190-
if "start" in args:
191-
if kwargs.get("timeout") is None or kwargs["timeout"] < timeout:
192-
kwargs["timeout"] = timeout
193-
kwargs.setdefault("stdin", subprocess.DEVNULL)
194-
kwargs["creationflags"] = kwargs.get("creationflags", 0) | _NO_WINDOW
195-
env = dict(kwargs.get("env") or os.environ)
196-
env.setdefault("PGCTLTIMEOUT", str(timeout))
197-
kwargs["env"] = env
198-
return original(args, **kwargs)
199-
200-
ps.pg_ctl = pg_ctl
201-
ps._audiomuse_pg_ctl_patched = True
188+
with _patch_lock:
189+
if getattr(ps, "_audiomuse_pg_ctl_patched", False):
190+
return
191+
original = ps.pg_ctl
192+
timeout = paths.pg_start_timeout()
193+
194+
def pg_ctl(args, **kwargs):
195+
if args and "start" in args:
196+
if kwargs.get("timeout") is None or kwargs["timeout"] < timeout:
197+
kwargs["timeout"] = timeout
198+
kwargs.setdefault("stdin", subprocess.DEVNULL)
199+
kwargs["creationflags"] = (kwargs.get("creationflags") or 0) | _NO_WINDOW
200+
env = kwargs.get("env")
201+
env = dict(os.environ if env is None else env)
202+
env.setdefault("PGCTLTIMEOUT", str(timeout))
203+
kwargs["env"] = env
204+
return original(args, **kwargs)
205+
206+
ps.pg_ctl = pg_ctl
207+
ps._audiomuse_pg_ctl_patched = True
202208

203209

204210
def start_embedded(data_dir):

0 commit comments

Comments
 (0)