RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.81k stars 76 forks source link

CLI is not thread-safe #154

Open RobertCraigie opened 2 years ago

RobertCraigie commented 2 years ago

Bug description

If binaries are missing, the CLI will naively attempt to download them, this can lead to OS errors if multiple processes are started at once.

How to reproduce

Ful reproduction is still in progress.

Encountered stack trace:

lint run-test-pre: commands[0] | python scripts/cleanup.py
lint run-test-pre: commands[1] | coverage run -m prisma generate --schema=tests/data/schema.prisma
[DEBUG  ] prisma.binaries.binaries: query-engine cached at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-query-engine-darwin
[DEBUG  ] prisma.binaries.binaries: migration-engine cached at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-migration-engine-darwin
[DEBUG  ] prisma.binaries.binaries: introspection-engine cached at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-introspection-engine-darwin
[DEBUG  ] prisma.binaries.binaries: prisma-fmt cached at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-prisma-fmt-darwin
[DEBUG  ] prisma.binaries.binaries: prisma-cli-darwin cached at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-cli-darwin
[DEBUG  ] prisma.binaries.binaries: All binaries are cached
[DEBUG  ] prisma.cli.prisma: Using Prisma CLI at /var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-cli-darwin
[DEBUG  ] prisma.cli.prisma: Running prisma command with args: ['generate', '--schema=tests/data/schema.prisma']
Traceback (most recent call last):
  File "/private/tmp/tox/prisma-client-py/lint/lib/python3.9/site-packages/prisma/__main__.py", line 6, in <module>
    cli.main()
  File "/private/tmp/tox/prisma-client-py/lint/lib/python3.9/site-packages/prisma/cli/cli.py", line 38, in main
    sys.exit(prisma.run(args[1:]))
  File "/private/tmp/tox/prisma-client-py/lint/lib/python3.9/site-packages/prisma/cli/prisma.py", line 44, in run
    process = subprocess.run(
  File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/var/folders/ql/0v8h20s972s6zz4t_3qc49bc0000gp/T/prisma/binaries/engines/1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85/prisma-cli-darwin'

Expected behavior

No errors.

Environment & setup

ezorita commented 6 months ago

Hi @RobertCraigie, I have a question related to this. I have observed that spawning multiple threads, each with its own client, sometimes generates a collision with an already open port. I had difficulties reproducing this, I guess it is a race condition produced when all of them attempt to create a connection at the same time.

The above could be easily solved with a semaphore, but I wonder if the client itself is thread safe, e.g. the same open client can be passed and used by multiple threads simultaneously, or are there stateful events in the client object that could result in a race condition?

Thanks!