|
26 | 26 | import os |
27 | 27 | import subprocess |
28 | 28 | import tempfile |
| 29 | +import threading |
29 | 30 | from urllib.parse import urlparse |
30 | 31 |
|
31 | 32 | from windows import paths |
|
35 | 36 | _USE_PGSERVER = None |
36 | 37 |
|
37 | 38 | _NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0) |
| 39 | +_patch_lock = threading.Lock() |
38 | 40 |
|
39 | 41 |
|
40 | 42 | def _check_pgserver(): |
@@ -183,22 +185,26 @@ def _patch_pgserver_pg_ctl(): |
183 | 185 | import pgserver.postgres_server as ps |
184 | 186 | if getattr(ps, "_audiomuse_pg_ctl_patched", False): |
185 | 187 | 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 |
202 | 208 |
|
203 | 209 |
|
204 | 210 | def start_embedded(data_dir): |
|
0 commit comments