Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 30, 2025

Resolves a race condition that occurred during validator shutdown where RPC requests could attempt to access the GlobalProofOfHistory singleton after it had already been shut down, causing a fatal error.

Problem

The issue manifested as:

Fatal error: GlobalProofOfHistory not initialized. Call initialize() first.

This occurred because:

  1. During shutdown, ValidatorCore::stop() calls GlobalProofOfHistory::shutdown() which resets the singleton
  2. RPC server requests (like getSlot) could still be processed after this point
  3. These requests call ValidatorCore::get_current_slot() which tries to access the now-destroyed GlobalProofOfHistory instance
  4. The instance() method throws an exception when the singleton is not initialized

Solution

Added safe initialization checking and graceful fallback behavior:

  1. Added is_initialized() method to GlobalProofOfHistory class for safe status checking
  2. Modified get_current_slot() methods to return default values (slot 0) instead of throwing exceptions when GlobalProofOfHistory is uninitialized
  3. Updated ValidatorCore methods to check initialization status before accessing GlobalProofOfHistory
  4. Added protective checks in block processing to handle uninitialized PoH gracefully

Changes

  • include/consensus/proof_of_history.h: Added is_initialized() static method
  • src/consensus/proof_of_history.cpp: Implemented thread-safe initialization checking and safe get_current_slot()
  • src/validator/core.cpp: Updated ValidatorCore methods to handle uninitialized PoH gracefully

Testing

  • ✅ All existing tests pass (validator, RPC, proof-of-history test suites)
  • ✅ Validator now shuts down cleanly without fatal errors
  • ✅ RPC methods return valid default values when PoH is unavailable
  • ✅ No breaking changes to existing functionality

The fix ensures the validator can handle RPC requests gracefully during startup and shutdown phases, eliminating the race condition while maintaining backward compatibility.

Fixes #30.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] GlobalProofOfHistory not initialized Fix GlobalProofOfHistory initialization race condition during shutdown Aug 30, 2025
Copilot AI requested a review from 0xrinegade August 30, 2025 18:09
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.

GlobalProofOfHistory not initialized

2 participants