Skip to content

Bug: Context getters have panics, silent overwrites, and inconsistent behavior #66

@odysseus0

Description

@odysseus0

Summary

The rpcserver context helpers have two concrete issues:

  1. Two functions panic on missing values
  2. Inconsistent error-handling across getters

Bug 1: Panics

// jsonrpc_server.go:432 - PANICS if sizeKey not in context
func GetRequestSize(ctx context.Context) int {
    return ctx.Value(sizeKey{}).(int)
}

// jsonrpc_server.go:436 - PANICS if urlKey not in context  
func GetURL(ctx context.Context) *url.URL {
    return ctx.Value(urlKey{}).(*url.URL)
}

Call these outside a handler (e.g., in a test, a goroutine, or refactored code path) and they crash.


Bug 2: Inconsistent Contracts

Function Missing Value Behavior
GetHighPriority Returns false
GetSigner Returns zero address
GetRequest Returns nil
GetBuilderNetSentAt Returns zero time
GetRequestSize Panics
GetURL Panics

Callers can't know which are safe without reading source.


Proposed Fix

Replace context.Value() getters with a typed wrapper (like Gin/Echo):

type RPCContext struct {
    Request     *http.Request  // guaranteed non-nil
    URL         *url.URL       // guaranteed non-nil  
    RequestSize int
    Signer      common.Address
    // ...
}

func (h *Handler) Method(rctx *RPCContext, args Args) (Result, error) {
    rctx.Request.Header.Get("X-Foo")  // always safe
}

No panics, no magic keys, explicit contracts.


Why Now

Hit this adding header logging across services. Every call site needed nil checks or risked panics. The current design makes simple tasks error-prone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions