fix(hit_testing): use flush() instead of commit() to preserve transaction context - #39221
Conversation
Pyrefly Type Coverage
|
…tion context HitTestingService.retrieve() called session.commit() after adding the DatasetQuery row, then passed the same session to compact_retrieve_response() which issued another query. Because the controller wraps the whole call in a transaction context manager, the explicit commit closed the transaction before the document lookup ran, raising: sqlalchemy.exc.InvalidRequestError: Can't operate on closed transaction inside context manager Replace commit() with flush() so the new row is materialised (gets a primary key, visible to subsequent reads within the same transaction) while leaving the outer context to manage the final commit. external_retrieve() also calls commit() but its follow-up (compact_external_retrieve_response) does not touch the session, so it is left unchanged. Fixes langgenius#38998 Signed-off-by: sergioperezcheco <checo520@outlook.com>
f199023 to
e1402d4
Compare
|
Hey @Dcsky777 — could you paste the exact error text you're seeing after the change? The screenshot is hard to read on my end. One common gotcha: if you're running Dify in Docker, editing the .py file on disk doesn't update the running api container — the worker process has the old bytecode cached. You'll need to restart the api worker (e.g. If the error message changed (or if it's now a different traceback), drop the text here and I'll take a look. The original "Can't operate on closed transaction inside context manager" should be fully resolved by the commit→flush swap, so if you're still seeing that exact message it almost certainly means the running process hasn't picked up the edit yet. |
|
I have indeed restarted all containers, yet the error Can't operate on closed transaction inside context manager still persists.
2186103696
***@***.***
|
|
Thanks for confirming the restart @Dcsky777. Since the error message is identical after the change, the running process almost certainly isn't executing the patched line. Can you verify by grepping the file inside the api container itself? Run this on the host that runs Docker: If you still see If instead you see |
|
output: |
|
Thanks for the grep output @Dcsky777 — that confirms it. Line 186 still shows To pick up the fix you have two options:
After either step, re-run |
|
You're really awesome. |
|
Glad it helped! If a maintainer has a minute to take a look I'd appreciate the review — happy to adjust anything if needed. |
|
Glad it fixed the issue! The change replaces two |

HitTestingService.retrieve() called session.commit() after adding the DatasetQuery row, then passed the same session to compact_retrieve_response() which issued another query. Because the controller wraps the whole call in a transaction context manager, the explicit commit closed the transaction before the document lookup ran, raising:
sqlalchemy.exc.InvalidRequestError: Can't operate on closed transaction inside context manager
Replace commit() with flush() so the new row is materialised (gets a primary key, visible to subsequent reads within the same transaction) while leaving the outer context to manage the final commit.
external_retrieve() also calls commit() but its follow-up compact_external_retrieve_response() does not touch the session, so it is left unchanged.
Fixes #38998