Open abrichr opened 4 days ago
@abrichr This looks like its because the session you are fetching recording.processed_action_events
from is closed, which means only direct relationships would work. action_event.screenshot
would work, but any relationships on screenshot
would fail, which is what is happening when cropped_image
is called. The solution could be that in get_window_segmentation
, you could create a read-only session that we can then attach the screenshot object to (session.add
should work). Let me know if that fixes it
@KIRA009 this should really be abstracted away somehow. Any idea when this regression was introduced? This code hasn't changed in a while.
Edit: it appears to have spontaneously resolved. The timesheet task has different timing than the calculator task (i.e. more segments take longer to describe). Could it be a race condition?
Doesn't really look like a race condition, especially because it looks like its running on the same process.
This code hasn't changed in a while.
Do you mean this was working after the changes we made to the different types of db sessions?
ChatGPT:
The DetachedInstanceError
in SQLAlchemy occurs when you try to access a related object or attribute that requires a database query, but the parent object is not attached to an active session. Here are steps to address this issue:
Make sure the session is still open when accessing the attribute.
Load the required attributes while the session is active.
Reattach the detached instance to an active session.
Use joinedload
to eagerly load the attribute when querying the parent instance.
from sqlalchemy.orm import joinedload
session = Session()
screenshot = session.query(Screenshot).options(joinedload(Screenshot.action_event)).filter_by(id=some_id).first()
Ensure the session is still open when accessing the lazy-loaded attribute.
session = Session()
screenshot = session.query(Screenshot).filter_by(id=some_id).first()
# Access the action_event attribute while the session is still open
action_event = screenshot.action_event
Reattach the detached instance to an active session before accessing the attribute.
session = Session()
screenshot = session.query(Screenshot).filter_by(id=some_id).first()
# Detach and later reattach the instance
session.expunge(screenshot)
# Reattach to a session
session.add(screenshot)
action_event = screenshot.action_event
Choose the method that best fits your use case.
I think we want to use joinedload
in the relevant functions in crud.py
.
This is happening because the session is closed at the end of https://github.com/OpenAdaptAI/OpenAdapt/blob/main/openadapt/models.py#L103
Describe the bug
To Reproduce