@@ -316,6 +316,8 @@ class Reply:
316316 """Provide access to the result of a function execution that got dispatched
317317 through WorkerPool.spawn()."""
318318
319+ _exception : BaseException | None = None
320+
319321 def __init__ (self , task , threadmodel : ExecModel ) -> None :
320322 self .task = task
321323 self ._result_ready = threadmodel .Event ()
@@ -328,10 +330,10 @@ def get(self, timeout: float | None = None):
328330 including its traceback.
329331 """
330332 self .waitfinish (timeout )
331- try :
333+ if self . _exception is None :
332334 return self ._result
333- except AttributeError :
334- raise self ._exc from None
335+ else :
336+ raise self ._exception . with_traceback ( self . _exception . __traceback__ )
335337
336338 def waitfinish (self , timeout : float | None = None ) -> None :
337339 if not self ._result_ready .wait (timeout ):
@@ -342,8 +344,9 @@ def run(self) -> None:
342344 try :
343345 try :
344346 self ._result = func (* args , ** kwargs )
345- except BaseException as exc :
346- self ._exc = exc
347+ except BaseException as e :
348+ # sys may be already None when shutting down the interpreter
349+ self ._exception = e
347350 finally :
348351 self ._result_ready .set ()
349352 self .running = False
@@ -526,7 +529,9 @@ def __init__(self, outfile, infile, execmodel: ExecModel) -> None:
526529 except (AttributeError , OSError ):
527530 pass
528531 self ._read = getattr (infile , "buffer" , infile ).read
529- self ._write = getattr (outfile , "buffer" , outfile ).write
532+ _outfile = getattr (outfile , "buffer" , outfile )
533+ self ._write = _outfile .write
534+ self ._flush = _outfile .flush
530535 self .execmodel = execmodel
531536
532537 def read (self , numbytes : int ) -> bytes :
@@ -544,7 +549,7 @@ def write(self, data: bytes) -> None:
544549 """Write out all data bytes."""
545550 assert isinstance (data , bytes )
546551 self ._write (data )
547- self .outfile . flush ()
552+ self ._flush ()
548553
549554 def close_read (self ) -> None :
550555 self .infile .close ()
0 commit comments