Skip to content

Refactor: release BackendManager lock before writing responses in route_request #138

Description

@coderabbitai

Background

Identified during code review of #128 (Define local daemon observability).

In crates/weaverd/src/dispatch/handler/mod.rs, route_request calls self.backends.with_backends(|backends| self.router.route(&request, writer, backends)). The with_backends closure holds the FusionBackends Mutex lock for the duration of the route call, which includes writing the response back to the client via ResponseWriter. Holding the lock during I/O operations increases lock-contention latency under concurrent connections and has no documented upper bound on hold duration.

No tests currently verify concurrent connection handling or lock behaviour under variable I/O latency.

Proposed direction

Refactor route_request to release the BackendManager lock before writing responses. Obtain the routing result inside the lock, then perform all ResponseWriter calls outside it. Add concurrent connection tests that verify correct behaviour and acceptable latency under variable I/O conditions. Document the intended maximum lock-hold duration as a constraint on the with_backends contract.

Affected files

  • crates/weaverd/src/dispatch/handler/mod.rs
  • crates/weaverd/src/dispatch/backend_manager.rs
  • Test files under crates/weaverd/src/tests/

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions