fix: [precaution fix] Capture Python futures while running in the background #365
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does the PR do?
The
py_future
object generated byRunCoroutine()
is lost upon the function returns if the coroutine is to be ran in the background. This can be problematic as some sources suggest therun_coroutine_threadsafe()
only maintain a weak reference to thepy_future
object that it returns. Therefore, this PR will implement the workaround suggested on the official Python documentation which stores thepy_future
in a Python set and remove it upon the done callback is invoked, so thepy_future
cannot be dangling while waiting for it to execute in the background.Checklist
<commit_type>: <Title>
Commit Type:
Check the conventional commit type
box here and add the label to the github PR.
Related PRs:
N/A
Where should the reviewer start?
N/A
Test plan:
No additional tests were added. The current tests, i.e. L0_backend_python should provide sufficient coverage for any new issue introduced by the change.
#15608296
Caveats:
We can explore if the Python library actually has weak reference while waiting for background futures.
Background
The future object returned from
asyncio.run_coroutine_threadsafe(...)
is not captured by the stub process while waiting for the future object to complete execution in the background. If therun_coroutine_threadsafe()
only maintains a weak reference to the future object, the future object may be deleted by the time the result is ready.Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)
N/A