Skip to content

Commit de3895d

Browse files
committed
Attempt to be a responsible subprocess user(use .communicate).
1 parent d271b10 commit de3895d

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

postgresql/cluster.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def __exit__(self, typ, val, tb):
192192

193193
def init(self,
194194
password = None,
195+
timeout = None,
195196
**kw
196197
):
197198
"""
@@ -253,36 +254,32 @@ def init(self,
253254
cmd,
254255
close_fds = close_fds,
255256
bufsize = 1024 * 5, # not expecting this to ever be filled.
256-
stdin = sp.PIPE,
257+
stdin = None,
257258
stdout = logfile,
258259
# stderr is used to identify a reasonable error message.
259260
stderr = sp.PIPE,
260261
)
261-
# stdin is not used; it is not desirable for initdb to be attached.
262-
p.stdin.close()
263262

264-
while True:
265-
try:
266-
rc = p.wait()
267-
break
268-
except OSError as e:
269-
if e.errno != errno.EINTR:
270-
raise
271-
finally:
272-
if p.stdout is not None:
273-
p.stdout.close()
263+
try:
264+
stdout, stderr = p.communicate(timeout=timeout)
265+
except sp.TimeoutExpired:
266+
p.kill()
267+
stdout, stderr = p.communicate()
268+
finally:
269+
rc = p.returncode
274270

275271
if rc != 0:
276272
# initdb returned non-zero, pickup stderr and attach to exception.
277273

278-
r = p.stderr.read().strip()
274+
r = stderr
279275
try:
280276
msg = r.decode('utf-8')
281277
except UnicodeDecodeError:
282278
# split up the lines, and use rep.
283279
msg = os.linesep.join([
284280
repr(x)[2:-1] for x in r.splitlines()
285281
])
282+
286283
raise InitDBError(
287284
"initdb exited with non-zero status",
288285
details = {
@@ -293,11 +290,6 @@ def init(self,
293290
creator = self
294291
)
295292
finally:
296-
if p is not None:
297-
for x in (p.stderr, p.stdin, p.stdout):
298-
if x is not None:
299-
x.close()
300-
301293
if supw_tmp is not None:
302294
n = supw_tmp.name
303295
supw_tmp.close()

0 commit comments

Comments
 (0)