Repository navigation
fix(api): answer 404, not 500, when a request loses a race to a delete - #812
Conversation
The admin API routes that read or remove one module checked that it existed, then looked it up again to act on it. A DELETE landing between the two made the second lookup throw, and the route answered 500 with "module name '<name>' does not exist". The __all metrics routes listed every policy first, so one deleted before its turn failed the whole scrape with 500. - AbstractManager::module_find_locked checks and gets a module in one step, and returns nullptr when it is missing. The tap and policy GETs, the default policy's bucket, window and Prometheus routes, and the per-policy JSON and Prometheus metrics use it, and answer 404 with the message each already gave for a missing module. - A single policy's metrics look the policy up before parsing the period, so a missing policy still answers 404 whatever the period. - __all skips a policy removed since it was listed, as the OpenTelemetry push does. - The tap and policy DELETEs answer 404 to the ModuleException that remove_tap and remove_policy throw, having changed nothing, when no module has the name. remove_tap checks for the tap before it waits for a policy load in progress, so a tap already missing is answered at once. Any other failure still answers 500. - The route bodies move to functions in visor::api, so tests can call them in the state a lost race leaves: the module gone after its name was checked or listed. - visor-core links the macOS frameworks that httplib's HTTPS client in CoreServer needs, which pktvisord only got through crashpad. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
/ai-review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
AI Code Review — risk tier: Advisory only. A human owns the merge decision. Summary — Net-positive fix: check-then-use races on module lookup/removal are closed by making the lookup atomic under the manager lock ( No findings. 🤖 AI Code Review · run · prompts: |
|
AI Code Review — risk tier: Advisory only. A human owns the merge decision. Summary — Net-positive change: it closes the check-then-act races where a concurrent DELETE caused a 500 instead of a 404, via 🤖 AI Code Review · run · prompts: |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
LCOV of commit
|
The admin API routes that read or remove one module checked that it existed, then looked it up again to act on it. A
DELETElanding between the two made the second lookup throw, so the route answered 500module name '<name>' does not existinstead of the 404 it gives for a module that never existed. The__allmetrics routes failed a whole scrape with 500 when one policy was deleted before its turn.A race the API can lose is a known, repeatable case, so it should never answer 500. A module that is gone answers 404.
Routes
GET /api/v1/taps/{name}tap does not existsDELETE /api/v1/taps/{name}tap does not exists(since #809; now tested)GET /api/v1/policies/{name}policy does not existsDELETE /api/v1/policies/{name}policy does not existsGET /api/v1/metrics/bucket/{n},/window/{n}(default policy)no "default" policy existsGET /api/v1/policies/{name}/metrics/(window|bucket)/{n}policy does not existGET /api/v1/policies/{name}/metrics/prometheuspolicy does not exists(text/plain)__allon both per-policy metrics routesEach route keeps the message it already used for a missing module. For a module that exists, or one that never existed, every route answers exactly as before: status, body and content type.
The fix
AbstractManager::module_find_locked(name)finds and locks a module in one step and returnsnullptrwhen it is missing. The routes use it instead ofmodule_existsfollowed bymodule_get_locked, so there's no gap between the check and the lookup, and the 404 no longer depends on exception types.ModuleExceptionthatremove_tapandremove_policythrow, having changed nothing, when no module has the name. Any other failure still answers 500. The contract is documented on both declarations.remove_tapchecks for the tap before it waits for a policy load, so a DELETE of a tap that's already missing answers at once instead of waiting behind a load. A load can read a whole pcap file while it holds that lock. A tap that exists still goes through the locked re-check.visor::api(CoreServer.h). The lambdas only parse the request, so tests can call a route in the state a lost race leaves.visor-corelinks CoreFoundation and Security on macOS. The test binary now linksCoreServer.cpp, whose HTTPS client loads keychain roots; pktvisord only got those frameworks through crashpad. Linux and Windows are unchanged.Tests
src/tests/test_core_server.cpp(new). Each case loads a module, removes it, then calls the route function and asserts the status and JSON error. It also checks a 200 before the removal. Covered:long,__allskipping a removed policy,__allskipping a policy without that period yet, and 425 for a single one.src/tests/test_policies.cpp:remove_policycalls: exactly one removal per round, andModuleExceptionfor the rest.module_get_locked(develop's behaviour);__allskip removed;Verification
unit-tests-input-netprobefails the same way on develop on this machine and passes in CI.--admin-api. Racing cycles each create a module, then release a DELETE together with GETs, DELETEs and metrics reads of the same name, while a scraper loops over the three__allroutes:__allscrapes)Behaviour to know
A DELETE of a tap that exists still waits for a policy load in progress, as since #809.
Not changed
POST races are fixed in a separate PR stacked on this one:
POST /api/v1/tapsandPOST /api/v1/policiesrender their response from pointers a concurrent DELETE can free;PolicyManager::loadchecks an input's existence and fetches it in two steps.🤖 Generated with Claude Code