Skip to content

Comments

fix: propagate context to GetVehicleForTrip to prevent db connection leaks#473

Open
ARCoder181105 wants to merge 1 commit intoOneBusAway:mainfrom
ARCoder181105:fix/context-propagation-getvehicle
Open

fix: propagate context to GetVehicleForTrip to prevent db connection leaks#473
ARCoder181105 wants to merge 1 commit intoOneBusAway:mainfrom
ARCoder181105:fix/context-propagation-getvehicle

Conversation

@ARCoder181105
Copy link
Contributor

Description

This PR resolves a stability issue where orphaned database queries were not properly cancelled when HTTP clients disconnected early. GetVehicleForTrip previously spun up an isolated context.Background() with a hardcoded timeout, leading to potential database connection pool exhaustion under heavy load or client disconnects.

Changes Made

  • Modified internal/gtfs/gtfs_manager.go: GetVehicleForTrip now accepts and wraps the HTTP request context.
  • Updated downstream API handlers (e.g., arrival_and_departure_for_stop_handler.go and arrivals_and_departure_for_stop.go) to properly pass r.Context() to GetVehicleForTrip.
  • Updated gtfs_manager_test.go to provide a context in test cases.
  • Ensured proper defer cancel() execution to prevent memory leaks on the context tree.
  • Verified that downstream SQLite GtfsDB.Queries successfully inherit the propagated context.
  • Ran make fmt, make lint, and make test successfully.

Closes #472

…leaks

- Updated `GetVehicleForTrip` in `gtfs_manager.go` to accept `context.Context` instead of using an isolated `context.Background()`.
- Updated downstream caller handlers to pass the HTTP request context.
- Ensures database queries tied to dropped HTTP requests are cancelled immediately, protecting the connection pool under heavy load.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Database connection leaks in GetVehicleForTrip due to isolated context

1 participant