You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/11/04 12:49:44 UTC

[GitHub] [airflow] mjpieters opened a new issue #12090: task_command.task_run log handling is either broken or redundant

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:
users@infra.apache.org



[GitHub] [airflow] kaxil closed issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
kaxil closed issue #12090:
URL: https://github.com/apache/airflow/issues/12090


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [airflow] mjpieters commented on issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
mjpieters commented on issue #12090:
URL: https://github.com/apache/airflow/issues/12090#issuecomment-724883070


   > Btw. if there will be more handlers for `airflow.task` I think we will end up with duplicated logs in webserver view, won't we?
   
   Only if those handlers write to the same log file. The webviewer displays for specific log files (via a Flask file-serving route), not to anything a handler might do. There are all sorts of handlers, not all write to log files.
   
   Or were you thinking of something else?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [airflow] mjpieters commented on issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
mjpieters commented on issue #12090:
URL: https://github.com/apache/airflow/issues/12090#issuecomment-721719841


   The `shutdown()` call was added [in this pull request](https://github.com/apache/airflow/pull/3259). I don't know if this still applies in Airflow 2.0.0, but I'm of the opinion that any logging shutdown should only be done when the process exits. It should not be up to `task_command.task_run()` to assume it is only ever used as a top-level call.
   
   In other words, if a `logging.shutdown()` call is necessary when, say, a forked child is about to exit, then the code triggering the exit should call `logging.shutdown()` just before it calls `os._exit()`.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [airflow] turbaszek commented on issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
turbaszek commented on issue #12090:
URL: https://github.com/apache/airflow/issues/12090#issuecomment-724712884


   Btw. if there will be more handlers for `airflow.task` I think we will end up with duplicated logs in webserver view, won't we?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [airflow] mjpieters commented on issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
mjpieters commented on issue #12090:
URL: https://github.com/apache/airflow/issues/12090#issuecomment-721721603


   I found [this review remark](https://github.com/apache/airflow/pull/3259#discussion_r184536876):
   
   > I removed this line in the Elasticsearch commit (because I saw this method registered in atexit()) and it causes NoneType error. My theory is that logging.shutdown() called in atexit() need settings.Session() to be defined but at the point it was called according to the order exit functions were registered, the orm was disposed. Explicitly calling it here is safe as all the task handler have logic prevent duplicate close() calls.
   
   The correct fix would have been to fix the  logger that requires a live session object, rather than shut down all logging. Barring that, only shut down the handler that causes the problems, not all logging.
   
   What exactly was causing a `NoneType` error at exit there, do you remember, @KevinYang21?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [airflow] turbaszek commented on issue #12090: task_command.task_run log handling is either broken or redundant

Posted by GitBox <gi...@apache.org>.
turbaszek commented on issue #12090:
URL: https://github.com/apache/airflow/issues/12090#issuecomment-725691765


   > Or were you thinking of something else?
   
   I was confused by custom code I was fixing on fork 😄 You are right, if we have separate handlers (different streams/files) everything works as expected. 
   


----------------------------------------------------------------
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:
users@infra.apache.org