o-nikolas commented on code in PR #67881:
URL: https://github.com/apache/airflow/pull/67881#discussion_r3455079550
##########
airflow-core/src/airflow/executors/local_executor.py:
##########
@@ -258,19 +261,40 @@ def end(self) -> None:
if proc.is_alive():
self.activity_queue.put(None)
- for proc in self.workers.values():
- if proc.is_alive():
- proc.join()
- proc.close()
+ # To prevent deadlock, we should consume results from result_queue
while waiting for processes to join.
+ # Otherwise, a worker blocked on putting results into a full
result_queue pipe will never exit,
+ # and an unbounded proc.join() will hang the scheduler indefinitely.
+ try:
+ for proc in self.workers.values():
+ if proc.is_alive():
+ while proc.is_alive():
+ self._read_results()
+ proc.join(timeout=0.05)
+ except (KeyboardInterrupt, SystemExit):
+ self.log.error("KeyboardInterrupt received during shutdown. Force
terminating workers.")
+ for p in self.workers.values():
+ if p.is_alive():
+ p.terminate()
+ p.join(timeout=0.2)
+ raise
+ finally:
+ # Process any extra results before closing
+ self._read_results()
- # Process any extra results before closing
- self._read_results()
+ for proc in self.workers.values():
+ with contextlib.suppress(ValueError):
+ proc.close()
- self.activity_queue.close()
- self.result_queue.close()
+ self.activity_queue.close()
+ self.result_queue.close()
def terminate(self):
- """Terminate the executor is not doing anything."""
+ """Terminate all worker processes under control of the executor
forcefully."""
+ self.log.info("Terminating all LocalExecutor worker processes.")
+ for proc in self.workers.values():
+ if proc.is_alive():
+ proc.terminate()
+ proc.join(timeout=0.2)
Review Comment:
This is for the happy case, if the worker is stuck or unresponsive you
should follow up with a kill/SIGKILL after the join if the SIGTERM was not
handled by the worker.
##########
airflow-core/src/airflow/executors/local_executor.py:
##########
@@ -258,19 +261,40 @@ def end(self) -> None:
if proc.is_alive():
self.activity_queue.put(None)
- for proc in self.workers.values():
- if proc.is_alive():
- proc.join()
- proc.close()
+ # To prevent deadlock, we should consume results from result_queue
while waiting for processes to join.
+ # Otherwise, a worker blocked on putting results into a full
result_queue pipe will never exit,
+ # and an unbounded proc.join() will hang the scheduler indefinitely.
+ try:
+ for proc in self.workers.values():
+ if proc.is_alive():
+ while proc.is_alive():
Review Comment:
I don't think you need both `is_alive` checks here.
##########
airflow-core/src/airflow/executors/local_executor.py:
##########
Review Comment:
Please add some testing for these changes. Both for the now active
`terminate()` and for the updates to `end()` and end-to-end test case for the
deadlock you're fixing would also be nice to ensure we don't regress.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]