Taragolis opened a new pull request, #37757:
URL: https://github.com/apache/airflow/pull/37757
<!--
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.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
This is attempt to prevent use `functools.lru_cache` or `functools.cache` on
methods which lead to memory leak, e.g. object can't be garbage collected if it
use methods decorated by this decorators.
Simple reproducible example
```python
import functools
class A:
def __init__(self):
self.a = {"a": 1, "b": 2}
@functools.lru_cache(maxsize=None)
def get_val(self, key: str):
return self.a[key]
for _ in range(100):
xxx = A()
xxx.get_val(key="a")
xxx.get_val("a")
xxx.get_val("b")
del xxx
import gc
gc.collect()
a_instances = [o for o in gc.get_objects() if isinstance(o, A)]
assert len(a_instances) == 0, len(a_instances)
# AssertionError: 100
```
Instead of that better to use use `functools.cached_property` (if it could
be a property), ruff [`B018`
rule](https://docs.astral.sh/ruff/rules/cached-instance-method/#example)
suggestion or use some third-party library, e.g.
[`methodtools`](https://github.com/youknowone/methodtools/)
```python
import methodtools
class A:
def __init__(self):
self.a = {"a": 1, "b": 2}
@methodtools.lru_cache(maxsize=None)
def get_val(self, key: str):
return self.a[key]
for _ in range(100):
xxx = A()
xxx.get_val(key="a")
xxx.get_val(key="a")
xxx.get_val("b")
del xxx
import gc
gc.collect()
a_instances = [o for o in gc.get_objects() if isinstance(o, A)]
assert len(a_instances) == 0, len(a_instances) # Do not raise assertion error
```
- ruff do not detect `compat` decorator `airflow.compat.functools.cache` for
avoid use it in methods use simple pre-commit check. It is only required until
we have Python 3.9 as min version, and do not use
`airflow.compat.functools.cache` in code base.
- There is couple of places where cache used into the Airflow core, some of
them might be a result of memory leakage, the simple way is use
`methodtools.lru_cache` - this one would not breaks public interfaces or
behaviour and do no required to rewrite. In another word we have bug fix "Here
and Now" instead of think how to rewrite without additional dependency.
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information.
In case of fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party
License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a
newsfragment file, named `{pr_number}.significant.rst` or
`{issue_number}.significant.rst`, in
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
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]