Blocking open calls in async functions (do not merge) - #4
Open
g-fabiani4-unipi wants to merge 2 commits into
Open
Blocking open calls in async functions (do not merge)#4g-fabiani4-unipi wants to merge 2 commits into
g-fabiani4-unipi wants to merge 2 commits into
Conversation
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


The objective of this PR is only to demonstrate the problem. Once we decide on a strategy I can work on that and make a new PR.
Description of the problem
upload_graph,get_graph_summary,get_node_types_countsandget_edge_types_countsare all defined as asynchronous but call the blocking methodopenfrom python standard IO.As per Ruff documentation on this issue ASYNC230
Troubleshooting
In 76d4cc5 I set up some logging in
get_node_types_countsandget_edges_types_countsin order to check when the functions are called and when the file is opened and closed. I left the blocking open calls where there are any.first-d3-2026as client and click the Update buttonIn 52a80a7 I used the asynchronous method
anyio.open_filefor opening the file in bothget_node_types_countsandget_edges_types_counts.first-d3-2026as client and click the Update buttonPossible solutions
A. Remove
asyncfrom path operation functions that need to use standard IOSince standard IO has no support for
await, we can simply declare path operation functions with justdefand be done with it.As per FastAPI documentation on concurrency:
B. Use an equivalent asynchronous function for opening files
Instead of
Use
As partially implemented in 52a80a7.
I don't see any problems with this solution. With the exception of
upload_graphwe are only opening the file for reading, so we don't have to concern ourselves with race conditions. In addition to that, the file is written into only the first time and never updated, so I think that it would make sense forupload_graphto open it in exclusive creation mode: