Skip to content

Commit ddeb593

Browse files
daiyippyglove authors
authored and
pyglove authors
committed
Fix stuck multiprocessing sandbox on large STDOUT.
PiperOrigin-RevId: 718981795
1 parent d31a119 commit ddeb593

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

pyglove/core/coding/execution.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io
1919
import multiprocessing
2020
import pickle
21+
import queue
2122
from typing import Any, Callable, Dict, Optional, Union
2223

2324
from pyglove.core import utils
@@ -197,28 +198,30 @@ def _run():
197198
q.put(e)
198199

199200
q = multiprocessing.Queue()
201+
p = multiprocessing.Process(
202+
target=_call, args=tuple([q] + list(args)), kwargs=kwargs
203+
)
200204
try:
201-
p = multiprocessing.Process(
202-
target=_call, args=tuple([q] + list(args)), kwargs=kwargs)
203205
p.start()
204-
p.join(timeout=timeout)
206+
x = q.get(timeout=timeout)
207+
except queue.Empty as e:
205208
if p.is_alive():
206209
# We use `kill` instead of `terminate` to release process resources
207210
# right away.
208211
p.kill()
209-
raise TimeoutError(f'Execution time exceed {timeout} seconds.')
210-
x = q.get()
211-
if isinstance(x, Exception):
212-
raise x
213-
try:
214-
return pickle.loads(x)
215-
except Exception as e:
216-
raise errors.SerializationError(
217-
'Cannot deserialize the output from sandbox.', e
218-
) from e
212+
raise TimeoutError(f'Execution time exceed {timeout} seconds.') from e
219213
finally:
220214
q.close()
221215

216+
if isinstance(x, Exception):
217+
raise x
218+
try:
219+
return pickle.loads(x)
220+
except Exception as e:
221+
raise errors.SerializationError(
222+
'Cannot deserialize the output from sandbox.', e
223+
) from e
224+
222225

223226
def maybe_sandbox_call(
224227
func: Callable[..., Any],

0 commit comments

Comments
 (0)