@@ -1541,7 +1541,7 @@ def init(redis_address=None,
1541
1541
_post_init_hooks = []
1542
1542
1543
1543
1544
- def shutdown ():
1544
+ def shutdown (exiting_interpreter = False ):
1545
1545
"""Disconnect the worker, and terminate processes started by ray.init().
1546
1546
1547
1547
This will automatically run at the end when a Python process that uses Ray
@@ -1553,7 +1553,17 @@ def shutdown():
1553
1553
defined remote functions or actors after calling ray.shutdown(), then you
1554
1554
need to redefine them. If they were defined in an imported module, then you
1555
1555
will need to reload the module.
1556
+
1557
+ Args:
1558
+ exiting_interpreter (bool): True if this is called by the atexit hook
1559
+ and false otherwise. If we are exiting the interpreter, we will
1560
+ wait a little while to print any extra error messages.
1556
1561
"""
1562
+ if exiting_interpreter and global_worker .mode == SCRIPT_MODE :
1563
+ # This is a duration to sleep before shutting down everything in order
1564
+ # to make sure that log messages finish printing.
1565
+ time .sleep (0.5 )
1566
+
1557
1567
disconnect ()
1558
1568
1559
1569
# Shut down the Ray processes.
@@ -1565,7 +1575,7 @@ def shutdown():
1565
1575
global_worker .set_mode (None )
1566
1576
1567
1577
1568
- atexit .register (shutdown )
1578
+ atexit .register (shutdown , True )
1569
1579
1570
1580
# Define a custom excepthook so that if the driver exits with an exception, we
1571
1581
# can push that exception to Redis.
@@ -1670,6 +1680,8 @@ def print_error_messages_raylet(task_error_queue, threads_stopped):
1670
1680
# messages originating from the worker.
1671
1681
while t + UNCAUGHT_ERROR_GRACE_PERIOD > time .time ():
1672
1682
threads_stopped .wait (timeout = 1 )
1683
+ if threads_stopped .is_set ():
1684
+ break
1673
1685
if t < last_task_error_raise_time + UNCAUGHT_ERROR_GRACE_PERIOD :
1674
1686
logger .debug ("Suppressing error from worker: {}" .format (error ))
1675
1687
else :
0 commit comments