Infisical / sdk

Infisical cross-language SDK
https://infisical.com
Other
21 stars 3 forks source link

Python/SDK: Critical errors with pyo3 #27

Open gaby opened 1 month ago

gaby commented 1 month ago

I have notice that when a server is running for +1 day using the infisical sdk errors will start showing up from pyo3 complaining about "No file handlers available". The only way to fix this is to restart the container running the python server.

This issue didn't happen with https://github.com/Infisical/infisical-python

Example Code (Throws error):

import time
from infisical_client import InfisicalClient, EncryptSymmetricOptions, DecryptSymmetricOptions

while True:
    #client = InfisicalClient()
    key = InfisicalClient().createSymmetricKey()
    encryptOptions = EncryptSymmetricOptions(
        key=key,
        plaintext="Infisical is awesome!"
    )

    encryptedData = InfisicalClient().encryptSymmetric(encryptOptions)

    decryptOptions = DecryptSymmetricOptions(
        ciphertext=encryptedData.ciphertext,
        iv=encryptedData.iv,
        tag=encryptedData.tag,
        key=key
    )

    decryptedString = InfisicalClient().decryptSymmetric(decryptOptions)
    print(f"Key={key}, cipher={decryptedString}")

Example Code (Works as intended):

import time
from infisical_client import InfisicalClient, EncryptSymmetricOptions, DecryptSymmetricOptions

while True:
    client = InfisicalClient()
    key = client.createSymmetricKey()
    encryptOptions = EncryptSymmetricOptions(
        key=key,
        plaintext="Infisical is awesome!"
    )

    encryptedData = client.encryptSymmetric(encryptOptions)

    decryptOptions = DecryptSymmetricOptions(
        ciphertext=encryptedData.ciphertext,
        iv=encryptedData.iv,
        tag=encryptedData.tag,
        key=key
    )

    decryptedString = client.decryptSymmetric(decryptOptions)
    print(f"Key={key}, cipher={decryptedString}")

@DanielHougaard Any ideas?

DanielHougaard commented 1 month ago

Hi @gaby, sorry for the delayed response. Are you able to share a stack trace or a more complete error? Is the python server running on Windows by any chance? I remember reading about an issue like this specifically on Windows machines/servers.

If you're running on Linux, can you please share more information about the distribution / image you're using?

gaby commented 1 month ago

@DanielHougaard I have updated the example code, it only happens if I create an InfisicalClient per call. If I do a global one this doesn't happen.

Stack Trace:

thread '<unnamed>' panicked at /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/scheduler/multi_thread/worker.rs:447:13:
OS can't spawn worker thread: Resource temporarily unavailable (os error 11)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/home/ubuntu/Desktop/git/fix/infisical-test.py", line 22, in <module>
    decryptedString = InfisicalClient().decryptSymmetric(decryptOptions)
  File "/usr/local/lib/python3.10/dist-packages/infisical_client/infisical_client.py", line 87, in decryptSymmetric
    result = self._run_command(Command(decrypt_symmetric=options))
  File "/usr/local/lib/python3.10/dist-packages/infisical_client/infisical_client.py", line 32, in _run_command
    response_json = self.inner.run_command(json.dumps(command.to_dict()))
pyo3_runtime.PanicException: OS can't spawn worker thread: Resource temporarily unavailable (os error 11)
DanielHougaard commented 1 month ago

That's interesting, thank you for the stack traces. I'm leaning towards it being a cleanup issue. The Python garbage collector should be freeing up the resources being used by the client at the end of the loop, but it appears that something is going wrong there. I'll need to look deeper into this before I can give you a detailed rundown of what's going wrong. I'll keep you in the loop!

Thank you for reporting this, @gaby