Fix database connection leaks in DEI routes and user CLI commands#3412
Conversation
…ase sessions and improve error handling Signed-off-by: Shlok Gilda <gildashlok@hotmail.com>
a421e93 to
f33054b
Compare
i think that table is unused and i plan to drop it soon lol (just filed #3423)
funny you say that. I'm fairly sure that the DatabaseSession object itself is causing leaks, even when used with Overall it seems like you are trying to do several things in this PR that may be better to split into separate, more focused PRs. |
|
Okay, yeah! The "too many things in one PR" sounds like me. I'll try to be more vigilant of this. Re DatabaseSession: I didn't check that. If that's the case, that should fix a whole bunch of issues. |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes database connection leaks in DEI API endpoints and user CLI commands by ensuring proper session cleanup in all code paths.
- Converted DEI route functions to use context managers for automatic session cleanup
- Added try-finally blocks to user CLI commands to guarantee session cleanup on early returns and exceptions
- Fixed missing import and removed non-functional SQLAlchemy 2.x code
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| augur/application/cli/user.py | Added try-finally blocks to add_user() and reset_password() commands to ensure session cleanup in all code paths (early returns, exceptions) |
| augur/api/routes/dei.py | Converted dei_track_repo() and dei_report() to use context managers for automatic session cleanup; fixed import statement; replaced non-functional session.autocommit = True with explicit session.commit() |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| new_user = User(login_name=username, login_hashword=password, email=email, text_phone=phone_number, first_name=firstname, last_name=lastname, admin=admin, tool_source="User CLI", tool_version=None, data_source="CLI") | ||
| session.add(new_user) | ||
| session.commit() | ||
| user_type = "admin user" if admin else "user" |
There was a problem hiding this comment.
Variable user_type is not used.
| user_type = "admin user" if admin else "user" |
|
@shlokgilda are there tests to confirm that the core of this change does in fact leak fewer connections? |
|
Hmm, good question! I don't think so. Might have to figure out a way to calculate the number of connections before/after the fix. Any suggestions? |
Description
dei_track_repo()anddei_report()to use context managers for automatic session cleanupadd_user()andreset_password()CLI commands to guarantee session cleanupdei_track_repo()session.autocommit = True(has no effect in SQLAlchemy 2.x) and added explicitsession.commit()to persist BadgingDEI recordsNotes for Reviewers
dei_report()scoping is critical: session must stay open during template rendering (lazy-loadsproject.repo), then closes before file operationsaugur/application/cli/db.py)augur user addandaugur user password_resetcommands - sessions properly close in all pathsdei_track_repo,dei_report) should be manually tested (although I don't think these are even used?)Signed commits