You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "MaslikovEgor (via GitHub)" <gi...@apache.org> on 2025/05/17 17:04:26 UTC

[PR] Fix memory leak in DagFileProcessorManager [airflow]

MaslikovEgor opened a new pull request, #50730:
URL: https://github.com/apache/airflow/pull/50730

   Fix memory leak in DagFileProcessorManager
   
   Wrap DagFileProcessorProcess.start() in try/except and ensure the logger filehandle is closed on startup failure to avoid file descriptor leaks. Update DagFileStat to record import errors and prevent tight retry loops, and switch metric tags to use string file paths.
   
   Closes: #50708
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #50730: Fix memory leak in DagFileProcessorManager
URL: https://github.com/apache/airflow/pull/50730


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "zachliu (via GitHub)" <gi...@apache.org>.
zachliu commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2894641248

   unfortunately this doesn't fix the small memory leak. i understand you are trying to properly cleanup everything upon the failure of `DagFileProcessorProcess.start()`, but it seems the leak doesn't come from file handles here
   
   to make sure everyone is on the same page. in https://github.com/apache/airflow/issues/50708, i meant whether `DagFileProcessorProcess.start()` failed or not, the memory always leaked. my guess is the leak point is somewhere among the initialization steps. possibly `airflow/__init__.py`


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "zachliu (via GitHub)" <gi...@apache.org>.
zachliu commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2899405578

   i may have identified the leak...
   i narrowed it down to these two library imports:
   
   ```python
   from importlib import metadata
   from sqlalchemy import create_engine
   ```
   
   each dag-processor's child process imports them but couldn't wipe clean the memory footprint. hence the "pinhole" leak
   if anyone can confirm this, i'll close https://github.com/apache/airflow/issues/50708


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #50730:
URL: https://github.com/apache/airflow/pull/50730#discussion_r2099906836


##########
airflow-core/src/airflow/dag_processing/manager.py:
##########
@@ -1034,10 +1054,11 @@ def _kill_timed_out_processors(self):
                 )
                 self._file_stats[file] = stat
 
-        # Clean up `self._processors` after iterating over it
+        # Clean up timed-out `self._processors` after iterating over it
         for proc in processors_to_remove:
-            processor = self._processors.pop(proc)
-            processor.logger_filehandle.close()
+            processor = self._processors.pop(proc, None)
+            if processor:
+                processor.logger_filehandle.close()

Review Comment:
   ```suggestion
               if processor := self._processors.pop(proc, None):
                   processor.logger_filehandle.close()
   ```
   
   There are elsewhere that can benefit from this pattern 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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "zachliu (via GitHub)" <gi...@apache.org>.
zachliu commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2939751502

   > @zachliu are you on track with this one? Any update or help needed?
   
   @amoghrajesh this pr doesn't seem to fix the memory leak. i created https://github.com/apache/airflow/issues/50708 three weeks ago and i have been trying to identifying the root cause since then...but it was all to no avail. yes i do need help :pray: 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "zachliu (via GitHub)" <gi...@apache.org>.
zachliu commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2891087300

   @MaslikovEgor out of curiosity, how did you discover this was the root cause of https://github.com/apache/airflow/issues/50708?


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2938822386

   @zachliu are you on track with this one? Any update or help needed?


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2888495033

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2942867202

   Thanks @zachliu, let me ask for some help :D 
    


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #50730:
URL: https://github.com/apache/airflow/pull/50730#discussion_r2099904795


##########
airflow-core/src/airflow/dag_processing/manager.py:
##########
@@ -884,32 +884,52 @@ def client(self) -> Client:
     def _create_process(self, dag_file: DagFileInfo) -> DagFileProcessorProcess:
         id = uuid7()
 
-        callback_to_execute_for_file = self._callback_to_execute.pop(dag_file, [])
+        callbacks = self._callback_to_execute.pop(dag_file, [])
         logger, logger_filehandle = self._get_logger_for_dag_file(dag_file)
 
-        return DagFileProcessorProcess.start(
-            id=id,
-            path=dag_file.absolute_path,
-            bundle_path=cast("Path", dag_file.bundle_path),
-            callbacks=callback_to_execute_for_file,
-            selector=self.selector,
-            logger=logger,
-            logger_filehandle=logger_filehandle,
-            client=self.client,
-        )
+        try:
+            # Attempt to start the subprocess for parsing
+            processor = DagFileProcessorProcess.start(
+                id=id,
+                path=dag_file.absolute_path,
+                bundle_path=cast("Path", dag_file.bundle_path),
+                callbacks=callbacks,
+                selector=self.selector,
+                logger=logger,
+                logger_filehandle=logger_filehandle,
+            )
+            return processor
+        except Exception as e:
+            # Clean up resources on failure to start
+            logger_filehandle.close()

Review Comment:
   Would it be better to put this in the `finally` block instead?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "MaslikovEgor (via GitHub)" <gi...@apache.org>.
MaslikovEgor commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-2893835797

   > @MaslikovEgor out of curiosity, how did you discover this was the root cause of #50708?
   
   I have faced the same issue with manager.py before that is why I think I am pretty familiar with this module. The leak is not big that is why I checked how resources cleaned


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "zachliu (via GitHub)" <gi...@apache.org>.
zachliu commented on code in PR #50730:
URL: https://github.com/apache/airflow/pull/50730#discussion_r2095743577


##########
airflow-core/src/airflow/dag_processing/manager.py:
##########
@@ -884,32 +884,52 @@ def client(self) -> Client:
     def _create_process(self, dag_file: DagFileInfo) -> DagFileProcessorProcess:
         id = uuid7()
 
-        callback_to_execute_for_file = self._callback_to_execute.pop(dag_file, [])
+        callbacks = self._callback_to_execute.pop(dag_file, [])
         logger, logger_filehandle = self._get_logger_for_dag_file(dag_file)
 
-        return DagFileProcessorProcess.start(
-            id=id,
-            path=dag_file.absolute_path,
-            bundle_path=cast("Path", dag_file.bundle_path),
-            callbacks=callback_to_execute_for_file,
-            selector=self.selector,
-            logger=logger,
-            logger_filehandle=logger_filehandle,
-            client=self.client,
-        )
+        try:
+            # Attempt to start the subprocess for parsing
+            processor = DagFileProcessorProcess.start(
+                id=proc_id,

Review Comment:
   ```suggestion
                   id=id,
   ```
   otherwise it's undefined



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "MaslikovEgor (via GitHub)" <gi...@apache.org>.
MaslikovEgor commented on code in PR #50730:
URL: https://github.com/apache/airflow/pull/50730#discussion_r2097609347


##########
airflow-core/src/airflow/dag_processing/manager.py:
##########
@@ -884,32 +884,52 @@ def client(self) -> Client:
     def _create_process(self, dag_file: DagFileInfo) -> DagFileProcessorProcess:
         id = uuid7()
 
-        callback_to_execute_for_file = self._callback_to_execute.pop(dag_file, [])
+        callbacks = self._callback_to_execute.pop(dag_file, [])
         logger, logger_filehandle = self._get_logger_for_dag_file(dag_file)
 
-        return DagFileProcessorProcess.start(
-            id=id,
-            path=dag_file.absolute_path,
-            bundle_path=cast("Path", dag_file.bundle_path),
-            callbacks=callback_to_execute_for_file,
-            selector=self.selector,
-            logger=logger,
-            logger_filehandle=logger_filehandle,
-            client=self.client,
-        )
+        try:
+            # Attempt to start the subprocess for parsing
+            processor = DagFileProcessorProcess.start(
+                id=proc_id,

Review Comment:
   @zachliu thank you!



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix memory leak in DagFileProcessorManager [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #50730:
URL: https://github.com/apache/airflow/pull/50730#issuecomment-3193995636

   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org