Skip to content

fix: dispose JsonDocument for batch JSON-RPC requests#10139

Closed
Snezhkko wants to merge 2 commits intoNethermindEth:masterfrom
Snezhkko:fix/jsonrpc-batch-jsondocument-dispose
Closed

fix: dispose JsonDocument for batch JSON-RPC requests#10139
Snezhkko wants to merge 2 commits intoNethermindEth:masterfrom
Snezhkko:fix/jsonrpc-batch-jsondocument-dispose

Conversation

@Snezhkko
Copy link
Contributor

@Snezhkko Snezhkko commented Jan 7, 2026

Previously, successful batch JSON-RPC requests never disposed the JsonDocument instance created during parsing. Single requests and error/limit paths correctly attached JsonDocument.Dispose() via JsonRpcResponse.AddDisposable, but the normal batch path only registered disposal for the ArrayPoolList of requests. As a result, the pooled JsonDocument buffers were effectively leaked, increasing GC and memory pressure under sustained batch load. This change attaches JsonDocument.Dispose() to the JsonRpcBatchResult lifetime so that the document is released when the batch result is disposed

Copy link
Member

@LukaszRozmej LukaszRozmej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check other potential problems with disposing?

// Increments failure metric and logs the exception, then stops processing.
Metrics.JsonRpcRequestDeserializationFailures++;
if (_logger.IsDebug) _logger.Debug($"Couldn't read request.{Environment.NewLine}{e}");
yield break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about here?

{
// Logs exception, then stop processing.
if (_logger.IsTrace) _logger.Trace($"Connection reset.{Environment.NewLine}{e}");
yield break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about here?

// Checks for deserialization failure and yields the result.
if (deserializationFailureResult.HasValue)
{
yield return deserializationFailureResult.Value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about here?

{
if (_logger.IsWarn) _logger.Warn($"The batch size limit was exceeded. The requested batch size {collection.Count}, and the current config setting is JsonRpc.{nameof(_jsonRpcConfig.MaxBatchSize)} = {_jsonRpcConfig.MaxBatchSize}.");
JsonRpcErrorResponse? response = _jsonRpcService.GetErrorResponse(ErrorCodes.LimitExceeded, "Batch size limit exceeded");
response.AddDisposable(() => jsonDocument.Dispose());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about collection here and in previous comments?

Copy link
Contributor Author

@Snezhkko Snezhkko Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about collection here and in previous comments?

Added disposal in the generic catch so we don’t leak JsonDocument / collection when DeserializeObjectOrArray throws, and removed the extra AddDisposable(() => collection.Dispose()) from the batch path — the collection is already disposed in IterateRequest’s finally, now both it and JsonDocument are disposed exactly once on every path.

@LukaszRozmej
Copy link
Member

I don't think this will work correctly

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a resource leak in batch JSON-RPC request handling by ensuring JsonDocument instances are properly disposed. Previously, only single requests and error paths registered JsonDocument disposal, while successful batch requests leaked the pooled document buffers, causing increased GC pressure under sustained batch load.

Changes:

  • Added JsonDocument disposal registration for batch requests via JsonRpcBatchResult
  • Added proper cleanup of both jsonDocument and collection in the exception handler

@LukaszRozmej
Copy link
Member

Replaced by #10207

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