mjpieters opened a new issue #12090:
URL: https://github.com/apache/airflow/issues/12090


   **Apache Airflow version**: 2.0.0a2
   
   As of #9363, the `tasks run` command attempts to restore the root logger 
handlers to the previous state after running the task with the handlers all 
replaced by those from the `airflow.tasks` logger.
   
   However, because no actual *copy* is created of the root handlers list, what 
you effectively end up with is either an empty list of handlers and nothing is 
restored. To make matters worse, the block ends with a `logging.shutdown()` 
call, which completely closes and releases all handlers.
   
   So, either the code to 'restore' can just be removed, or a proper copy needs 
to be made and the `logging.shutdown()` call needs to be removed from the 
command. `logging.shutdown` is going to be called when the interpreter exits, 
anyway.
   
   In detail, this is the code section:
   
   
https://github.com/apache/airflow/blob/bec9f3b29fd42ecd1beae3db75784b9a726caf15/airflow/cli/commands/task_command.py#L194-L222
   
   The first problem here is that `root_logger_handlers = root_logger.handlers` 
merely creates a reference to the logger `handlers` list. The 
`root_logger.removeHandler(handler)` in a loop further on remove the handlers 
from that same list as you iterate, and this causes it to [only remove *every 
second 
handler*](https://sopython.com/canon/95/removing-items-from-a-list-while-iterating-over-the-list/).
 Luckily, this was never a problem because in the standard configuration there 
is only a single handler on the root. If there are more, there is a bigger 
problem further down.
   
   Continuing on, the next loop, adding handlers from 
`airflow_logger_handlers`, causes those same handlers to show up in the 
`root_logger_handlers` reference, but those very same handlers are removed 
again in a second loop over the same `airflow_logger_handlers` list.
   
   So when you hit the final `for handler in root_logger_handlers: 
root_logger.addHandler(handler)` loop, either `root_logger_handlers` is an 
empty list (no handlers added), or, if you started with more than one root 
handler and every second handler was left in place, you now are adding handlers 
to the list that are already in the list.  The `Logger.addHandler()` method 
uses `self.handler.append()`, appending to a list you are iterating over, and 
so you have an infinite loop on your hands.
   
   Either just remove all handlers from the root logger (taking care to loop 
over the list in reverse, e.g. `for h in reversed(root_logger.handlers): 
root_logger.removeHandler(h)`), or create proper copy of the list with 
`root_logger_handlers = root_logger.handlers[:]`. The `logging.shutdown()` call 
has to be removed then too.
   
   
   
   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to