Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(jsonrpc): isolate pipelined request timeouts
  • Loading branch information
XnLemon committed Aug 12, 2026
commit fb91f6c9ee0c608de1cc07d2b976496b1b4776df
19 changes: 2 additions & 17 deletions protocol/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
once sync.Once

sync.RWMutex
wg sync.WaitGroup
timeout time.Duration
wg sync.WaitGroup
}

// NewServer creates new JSON RPC server.
Expand Down Expand Up @@ -103,17 +102,6 @@
// Register this after the cleanup defer so LIFO ordering cancels request contexts before Wait.
defer connectionCancel()
Comment thread
XnLemon marked this conversation as resolved.
Resolved

setTimeout := func(conn net.Conn, timeout time.Duration) {
t := time.Time{}
if timeout > time.Duration(0) {
t = time.Now().Add(timeout)
}

if err := conn.SetDeadline(t); err != nil {
logger.Errorf("[Jsonrpc][Server] connection.SetDeadline failed, t=%v, err=%v", t, err)
}
}

limitedReader := &io.LimitedReader{R: conn}
bufReader := bufio.NewReader(limitedReader)
var sequence uint64
Expand Down Expand Up @@ -144,7 +132,6 @@
}
reqHeader["HttpMethod"] = r.Method

httpTimeout := s.timeout
contentType := reqHeader[ContentTypeHeader]
mediaType, _, parseErr := mime.ParseMediaType(contentType)
unsupportedContentType := parseErr != nil || (mediaType != "application/json" && mediaType != "application/json-rpc")
Expand All @@ -157,31 +144,29 @@
if len(reqHeader["Timeout"]) > 0 {
timeout, err := time.ParseDuration(reqHeader["Timeout"])
if err == nil {
httpTimeout = timeout
ctx, timeoutCancel = context.WithTimeout(ctx, httpTimeout)
ctx, timeoutCancel = context.WithTimeout(ctx, timeout)
}
delete(reqHeader, "Timeout")
}
setTimeout(conn, httpTimeout)

requestSequence := sequence
sequence++
requestWG.Add(1)
go func(ctx context.Context, requestCancel, timeoutCancel context.CancelFunc, header map[string]string, body []byte,
Comment thread
XnLemon marked this conversation as resolved.
Resolved
Comment thread
XnLemon marked this conversation as resolved.
Resolved
responseHeader http.Header, contentType string, unsupportedContentType bool, requestSequence uint64) {
defer requestWG.Done()
defer requestCancel()
if timeoutCancel != nil {
defer timeoutCancel()
}

response := buildOrderedResponse(ctx, header, body, responseHeader, contentType,
unsupportedContentType, requestSequence)
select {
case responses <- response:
case <-connectionCtx.Done():
}
}(ctx, requestCancel, timeoutCancel, reqHeader, reqBody, r.Header, contentType, unsupportedContentType, requestSequence)

Check warning on line 169 in protocol/jsonrpc/server.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 9 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ_0X3ucnthQh2MXPQqQ&open=AZ_0X3ucnthQh2MXPQqQ&pullRequest=3579
}
}

Expand Down Expand Up @@ -355,7 +340,7 @@
})
}

func serveRequest(ctx context.Context, header map[string]string, body []byte, writer io.Writer) error {

Check failure on line 343 in protocol/jsonrpc/server.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ_0X3ucnthQh2MXPQqR&open=AZ_0X3ucnthQh2MXPQqR&pullRequest=3579
Comment thread
XnLemon marked this conversation as resolved.
Resolved
sendErrorResp := func(header map[string]string, body []byte) error {
rsp := &http.Response{
Header: make(http.Header),
Expand Down
109 changes: 109 additions & 0 deletions protocol/jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,112 @@ func TestHandlePkgPreservesPipelinedResponseOrder(t *testing.T) {
require.Equal(t, 2, second.id)
require.Equal(t, "fast", second.result)
}

type requestTimeoutInvoker struct {
base.BaseInvoker
longStarted chan struct{}
shortTimedOut chan struct{}
longCanceled chan struct{}
releaseLong chan struct{}
}

func (i *requestTimeoutInvoker) Invoke(ctx context.Context, invocation base.Invocation) result.Result {
switch invocation.MethodName() {
case "Long":
close(i.longStarted)
select {
case <-ctx.Done():
close(i.longCanceled)
return &result.RPCResult{Err: ctx.Err()}
case <-i.releaseLong:
return &result.RPCResult{Rest: "long"}
}
case "Short":
<-ctx.Done()
close(i.shortTimedOut)
return &result.RPCResult{Err: ctx.Err()}
default:
return &result.RPCResult{Err: fmt.Errorf("unexpected method %s", invocation.MethodName())}
}
}

func TestHandlePkgIsolatesPipelinedRequestTimeouts(t *testing.T) {
const servicePath = "request-timeout-test"
protocol := GetProtocol().(*JsonrpcProtocol)
invoker := &requestTimeoutInvoker{
BaseInvoker: *base.NewBaseInvoker(common.NewURLWithOptions(common.WithProtocol(JSONRPC))),
longStarted: make(chan struct{}),
shortTimedOut: make(chan struct{}),
longCanceled: make(chan struct{}),
releaseLong: make(chan struct{}),
}
protocol.SetExporterMap(servicePath, NewJsonrpcExporter(servicePath, invoker, protocol.ExporterMap()))
t.Cleanup(func() { protocol.ExporterMap().Delete(servicePath) })

serverConn, clientConn := net.Pipe()
handleDone := make(chan struct{})
go func() {
NewServer().handlePkg(serverConn)
close(handleDone)
}()

var releaseOnce sync.Once
releaseLong := func() { releaseOnce.Do(func() { close(invoker.releaseLong) }) }
t.Cleanup(func() {
releaseLong()
_ = clientConn.Close()
select {
case <-handleDone:
case <-time.After(time.Second):
t.Error("connection handler did not exit")
}
})
require.NoError(t, clientConn.SetReadDeadline(time.Now().Add(3*time.Second)))

makeRequest := func(method string, id int, timeout time.Duration) string {
body := fmt.Sprintf(`{"jsonrpc":"2.0","method":%q,"params":[],"id":%d}`, method, id)
return "POST /" + servicePath + " HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Type: application/json\r\n" +
"Timeout: " + timeout.String() + "\r\n" +
"Content-Length: " + fmt.Sprint(len(body)) + "\r\n\r\n" + body
}

writeDone := make(chan error, 1)
go func() {
_, err := clientConn.Write([]byte(
makeRequest("Long", 1, 5*time.Second) + makeRequest("Short", 2, 50*time.Millisecond),
))
writeDone <- err
}()

select {
case <-invoker.longStarted:
case <-time.After(time.Second):
t.Fatal("long invocation was not started")
}
select {
case <-invoker.shortTimedOut:
case <-time.After(time.Second):
t.Fatal("short invocation did not reach its context deadline")
}
require.NoError(t, <-writeDone)
select {
case <-invoker.longCanceled:
t.Fatal("short request timeout canceled the long request")
case <-time.After(250 * time.Millisecond):
}

releaseLong()
reader := bufio.NewReader(clientConn)
for id := 1; id <= 2; id++ {
httpResponse, err := http.ReadResponse(reader, nil)
require.NoError(t, err)
var payload struct {
ID int `json:"id"`
}
require.NoError(t, json.NewDecoder(httpResponse.Body).Decode(&payload))
require.NoError(t, httpResponse.Body.Close())
require.Equal(t, id, payload.ID)
}
}
Loading