Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChaCha20Poly1305 memory leak #9255

Closed
PaulWhitingS2 opened this issue Jul 17, 2023 · 2 comments · Fixed by #9272
Closed

ChaCha20Poly1305 memory leak #9255

PaulWhitingS2 opened this issue Jul 17, 2023 · 2 comments · Fixed by #9272
Labels

Comments

@PaulWhitingS2
Copy link

Cryptography 39.0.0 introduced a memory leak in the ChaCha20Poly1305 class. It is still present in the most recent release (41.0.2).

  1. python -m venv venv
  2. source venv/bin/activate
  3. pip install cryptography
  4. Run the following script. Monitor memory usage using top.
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
key = ChaCha20Poly1305.generate_key()
while True:
        chacha = ChaCha20Poly1305(key)

Cryptography 38.0.4 - Memory usage remains constant.
Cryptography >= 39.0.0 - Memory usage increases until the process is killed.

@reaperhulk
Copy link
Member

Thanks for the report. While we chase this down, here is a reduced reproducer:

import gc

from cryptography.hazmat.bindings._rust import FixedPool


class Foo:
    def __init__(self):
        self._pool = FixedPool(self._method)

    def _method(self):
        pass


while True:
    Foo()
    gc.collect()

@reaperhulk
Copy link
Member

It appears we need to implement __traverse__ and __clear__ so that the GC can properly collect everything (as there are circular references with the fixed pool). We'll get a fix up soon.

reaperhulk added a commit to reaperhulk/cryptography that referenced this issue Jul 20, 2023
reaperhulk added a commit to reaperhulk/cryptography that referenced this issue Jul 28, 2023
alex pushed a commit that referenced this issue Jul 28, 2023
* fix the memory leak in fixedpool

fixes #9255

* simplify fix
reaperhulk added a commit to reaperhulk/cryptography that referenced this issue Jul 29, 2023
* fix the memory leak in fixedpool

fixes pyca#9255

* simplify fix
alex pushed a commit that referenced this issue Jul 29, 2023
* fix the memory leak in fixedpool (#9272)

* fix the memory leak in fixedpool

fixes #9255

* simplify fix

* resolve clippy warnings
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

2 participants