Fix/discovery panic hardening#212
Open
EddieHouston wants to merge 2 commits intoBlockstream:new-indexfrom
Open
Fix/discovery panic hardening#212EddieHouston wants to merge 2 commits intoBlockstream:new-indexfrom
EddieHouston wants to merge 2 commits intoBlockstream:new-indexfrom
Conversation
Three tests are intentionally failing against the current code: - remove_unhealthy_service_missing_service_does_not_panic (assert! panic) - poisoned_healthy_lock_does_not_panic_on_get_servers (unwrap on poisoned lock) - poisoned_queue_lock_does_not_panic_on_run_health_check (unwrap on poisoned lock) Also marks the pre-existing live-network test with #[ignore] so it does not run in CI.
Replace assert! with warn! in remove_unhealthy_service, recover poisoned RwLocks via into_inner() throughout, and wrap the jobs loop body in catch_unwind so a panic cannot kill the discovery thread. Log the panic payload on catch so errors are actionable in production. Add comments documenting the two-layer defense, the lock-poison recovery tradeoff, and the single-consumer TOCTOU assumption.
0307c74 to
685527a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses a panic seen in production in the
discovery-jobsthread that was visible in logs. The code had a comment:// FIXME This was an unreachable but it was reached.assert!with warn log inremove_unhealthy_service—assert!(server.services.remove(&job.service))panicked when the service was absent from the healthy set due to corrupted state; now logs a warning and continues.elseFIXME is upgraded to a structured warn with hostname and addr.RwLocks — all.unwrap()calls onqueueandhealthylock acquisitions replaced with.unwrap_or_else(|e| e.into_inner())so a prior panic doesn't permanently wedge the manager.catch_unwind—spawn_jobs_threadnow catches any unhandled panic per iteration, logs an error, and keeps the thread alive..pop().unwrap()—run_health_checkused a TOCTOU pattern (peek under read lock, then pop under write lock); replaced withlet-elsethat returns early if the queue drains between the two lock acquisitions.save_healthy_serviceandremove_unhealthy_service.Tests
Commit history is structured as tests-first: the test commit introduces 3 intentionally failing tests against the original code, and the fix commit turns them all green.
These tests require
--features electrum-discoveryto compile and run — the same flag needed to build the discovery module itself. CI does not currently run with this flag, so to verify locally:cargo test --lib --features electrum-discovery -- discovery
remove_unhealthy_service_missing_service_does_not_panicremove_unhealthy_service_missing_addr_does_not_panicremove_unhealthy_service_removes_server_when_last_service_goneremove_unhealthy_service_keeps_server_when_other_services_remainpoisoned_healthy_lock_does_not_panic_on_get_serverspoisoned_queue_lock_does_not_panic_on_run_health_checkThe pre-existing live-network test is marked
#[ignore]so it is skipped during local test runs unless explicitly opted in with-- --ignored.