pyo3: RuntimeError: Already borrowed when using context managers
Bug Description
This issue was originally reported in watchfiles, but the maintainer there said that it is probably an issue with this lib, maybe related to https://github.com/PyO3/pyo3/issues/1205
The issue is that I get RuntimeError: Already borrowed when exiting a code that uses a context manager
Steps to Reproduce
I created this small script which will raise the issue everytime I finish the process:
import asyncio
import signal
import watchfiles
async def watch_files():
async for changes in watchfiles.awatch("/tmp/foobar"):
pass
async def main():
exit_event = asyncio.Event()
loop = asyncio.get_running_loop()
for s in [signal.SIGINT, signal.SIGTERM]:
loop.add_signal_handler(s, exit_event.set)
print("1) Initiating watch_files")
task = asyncio.create_task(watch_files())
await exit_event.wait()
print("2) SIGINT detected, canceling the task")
task.cancel()
done_event = asyncio.Event()
task.add_done_callback(lambda t: done_event.set())
print("3) Waiting for the task to finish")
await done_event.wait()
print("4) task finished")
if __name__ == "__main__":
print("** Before main")
asyncio.run(main())
print("** After main")
Just create a venv, install watchfiles in it and run the script. Close it with ctrl+C or by sending or kill -2 <PID>
Backtrace
future: <Task finished name='Task-9' coro=<Agent.process_door_events() done, defined at /home/bellini/dev/2u/nomad/nomad/agents/base.py:176> exception=RuntimeError('Already borrowed')>
Traceback (most recent call last):
File "/home/bellini/dev/2u/nomad/nomad/agents/base.py", line 181, in process_door_events
async for door_opened in self.listen_for_door_events():
File "/home/bellini/dev/2u/nomad/nomad/agents/dev.py", line 66, in listen_for_door_events
async for changes in watchfiles.awatch(self._incoming_file):
File "/home/bellini/dev/2u/nomad/.venv/lib/python3.10/site-packages/watchfiles/main.py", line 222, in awatch
with RustNotify([str(p) for p in paths], debug, force_polling, poll_delay_ms) as watcher:
RuntimeError: Already borrowed
Your operating system and version
Debian Sid
Your Python version (python --version)
Python 3.10.5
Your Rust version (rustc --version)
rustc 1.59.0
Your PyO3 version
The one used by watchfiles
How did you install python? Did you use a virtualenv?
apt
Additional Info
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 27 (20 by maintainers)
ok i will try over next few weekends… and if i get it done in docker you will get dockerfile as well (to help fixing it)
@bellini666 Try
pip install -e ..@07pepa The next step here is still https://github.com/PyO3/pyo3/issues/2525#issuecomment-1205809403 done by someone who is able to reproduce this, so that we can get a backtrace of the Rust side to figure where the exception originates and determine whether something in PyO3 or in watchfiles needs to be changed.