{"record":{"id":"9abb2596055b8676","repo":"hyperledger/fabric","slug":"context-finished-before-block-retrieved","errorCode":null,"errorMessage":"context finished before block retrieved","messagePattern":"context finished before block retrieved","errorType":"http","errorClass":null,"httpStatus":500,"severity":"warning","filePath":"common/deliver/deliver.go","lineNumber":310,"sourceCode":"\t\t\tif number > chain.Reader().Height()-1 {\n\t\t\t\tlogger.Warningf(\"[channel: %s] Block %d not found, block number greater than chain length bounds\", chdr.ChannelId, number)\n\t\t\t\treturn cb.Status_NOT_FOUND, nil\n\t\t\t}\n\t\t}\n\n\t\tvar block *cb.Block\n\t\tvar status cb.Status\n\n\t\titerCh := make(chan struct{})\n\t\tgo func() {\n\t\t\tblock, status = cursor.Next()\n\t\t\tclose(iterCh)\n\t\t}()\n\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\tlogger.Debugf(\"Context canceled, aborting wait for next block\")\n\t\t\treturn cb.Status_INTERNAL_SERVER_ERROR, errors.Wrapf(ctx.Err(), \"context finished before block retrieved\")\n\t\tcase <-erroredChan:\n\t\t\t// TODO, today, the only user of the errorChan is the orderer consensus implementations.  If the peer ever reports\n\t\t\t// this error, we will need to update this error message, possibly finding a way to signal what error text to return.\n\t\t\tlogger.Warningf(\"Aborting deliver for request because the backing consensus implementation indicates an error\")\n\t\t\treturn cb.Status_SERVICE_UNAVAILABLE, nil\n\t\tcase <-iterCh:\n\t\t\t// Iterator has set the block and status vars\n\t\t}\n\n\t\tif status != cb.Status_SUCCESS {\n\t\t\tlogger.Warningf(\"[channel: %s] Error reading from channel, cause was: %v\", chdr.ChannelId, status)\n\t\t\treturn status, nil\n\t\t}\n\n\t\t// increment block number to support FAIL_IF_NOT_READY deliver behavior\n\t\tnumber++\n\n\t\tif err := accessControl.Evaluate(); err != nil {","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/deliver/deliver.go#L292-L328","documentation":"deliverBlocks waits on a select for the next block, context cancellation, or a consensus error. When ctx is canceled or times out while waiting for the iterator's next block, the handler returns INTERNAL_SERVER_ERROR wrapping ctx.Err() with this message.","triggerScenarios":"Client context deadline exceeded or explicit cancel while the deliver stream is blocked waiting for the next block from the ledger iterator.","commonSituations":"Client-side gRPC deadline too short relative to block production rate; seek on a height that isn't produced for a long time; orderer slow to commit; network partitions.","solutions":["Increase the client's gRPC deadline/timeout for the deliver stream","Seek to a valid, existing block height (e.g., oldest/newest) rather than waiting on an unproduced height","Check orderer health (consensus, Raft leader) and network connectivity; retry with backoff"],"exampleFix":"// before\nctx := context.Background()\nstream, _ := client.Deliver(ctx) // no deadline\n// after\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)\ndefer cancel()\nstream, _ := client.Deliver(ctx)","handlingStrategy":"try-catch","validationCode":"if deadline, ok := ctx.Deadline(); ok && time.Until(deadline) < 30*time.Second {\n    return errors.New(\"deliver deadline too short; extend client timeout\")\n}","typeGuard":null,"tryCatchPattern":"status, err := deliverClient.Deliver(ctx, envelope)\nif err != nil && strings.Contains(err.Error(), \"context finished before block retrieved\") {\n    if errors.Is(err, context.DeadlineExceeded) {\n        return retryWithLongerDeadline(ctx)\n    }\n    return err // explicit cancel\n}","preventionTips":["Use generous deadlines on deliver streams (blocks may be slow to produce)","Retry with exponential backoff on context deadline errors","Check orderer/consensus health when waits are frequently interrupted"],"tags":["grpc","deliver","context-cancel","timeout"],"backgroundTag":"context-canceled","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}