Skip to content

Recover from panic in HandleTransaction to prevent peer crash#5472

Merged
pfi79 merged 1 commit into
hyperledger:mainfrom
Jaskirat-s7:fix/recover-panic-on-iterator-close-5048
May 6, 2026
Merged

Recover from panic in HandleTransaction to prevent peer crash#5472
pfi79 merged 1 commit into
hyperledger:mainfrom
Jaskirat-s7:fix/recover-panic-on-iterator-close-5048

Conversation

@Jaskirat-s7
Copy link
Copy Markdown
Contributor

Summary

When a transaction times out during a range query, the timeout path in Execute() closes LevelDB iterators via CloseQueryIterators() while the handler goroutine (spawned by handleMessageReadyState) is still actively calling iter.Next(). This causes a nil pointer dereference in the LevelDB iterator that crashes the peer process.

Changes

  • Added a deferred recover() in HandleTransaction (core/chaincode/handler.go) to catch panics from concurrent iterator access and return an error response instead of crashing the peer
  • Added regression tests that verify the panic is recovered and an error response is sent, and that the transaction ID is properly deregistered

Testing

All 320 existing tests pass, including the 2 new regression tests:

@Jaskirat-s7 Jaskirat-s7 requested a review from a team as a code owner May 6, 2026 05:39
@Jaskirat-s7 Jaskirat-s7 force-pushed the fix/recover-panic-on-iterator-close-5048 branch from f5f6a65 to 1c22fe3 Compare May 6, 2026 05:41
Comment thread core/chaincode/handler.go
Comment thread core/chaincode/handler.go Outdated
@Jaskirat-s7 Jaskirat-s7 force-pushed the fix/recover-panic-on-iterator-close-5048 branch from 1c22fe3 to 276964a Compare May 6, 2026 10:25
Comment thread core/chaincode/handler.go Outdated
When a transaction times out during a range query, the timeout path
closes LevelDB iterators while the handler goroutine is still using
them, causing a nil pointer dereference that crashes the peer.

Add a deferred recover() in HandleTransaction to catch such panics
and return an error response instead of crashing. This is safe because
the transaction has already timed out and the resources are being freed.

The panic value is logged but not included in the response payload to
avoid violating endorsement determinism across peers. Metrics are
recorded in the recover path to match the normal code path.

Also adds regression tests that verify the panic is recovered and
an error response is sent.

Fixes hyperledger#5048

Signed-off-by: Jaskirat-s7 <jaskiratsingh7812@gmail.com>
@Jaskirat-s7 Jaskirat-s7 force-pushed the fix/recover-panic-on-iterator-close-5048 branch from 276964a to baad0b4 Compare May 6, 2026 17:40
@pfi79 pfi79 enabled auto-merge (squash) May 6, 2026 17:51
@pfi79 pfi79 disabled auto-merge May 6, 2026 18:17
@pfi79 pfi79 merged commit 6a67397 into hyperledger:main May 6, 2026
15 checks passed
@pfi79
Copy link
Copy Markdown
Contributor

pfi79 commented May 7, 2026

@Mergifyio backport release-2.5

@mergify
Copy link
Copy Markdown

mergify Bot commented May 7, 2026

backport release-2.5

✅ Backports have been created

Details

pfi79 pushed a commit that referenced this pull request May 7, 2026
…#5473)

When a transaction times out during a range query, the timeout path
closes LevelDB iterators while the handler goroutine is still using
them, causing a nil pointer dereference that crashes the peer.

Add a deferred recover() in HandleTransaction to catch such panics
and return an error response instead of crashing. This is safe because
the transaction has already timed out and the resources are being freed.

The panic value is logged but not included in the response payload to
avoid violating endorsement determinism across peers. Metrics are
recorded in the recover path to match the normal code path.

Also adds regression tests that verify the panic is recovered and
an error response is sent.

Fixes #5048


(cherry picked from commit 6a67397)

Signed-off-by: Jaskirat-s7 <jaskiratsingh7812@gmail.com>
Co-authored-by: Jaskirat Singh <jaskiratsingh7812@gmail.com>
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.

2 participants