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(context): address PR review cancellation feedback
  • Loading branch information
XnLemon committed Jul 31, 2026
commit 2b2da28282ff402543870073491e48ce50c3afa5
3 changes: 3 additions & 0 deletions metadata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func GetMetadataFromRpcWithContext(ctx context.Context, revision string, instanc
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return nil, err
}
url, err := buildStandardMetadataServiceURL(instance)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions protocol/dubbo/dubbo_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ func (di *DubboInvoker) Invoke(ctx context.Context, ivc base.Invocation) result.
timeout := di.getTimeout(inv)
if async {
if callBack, ok := inv.CallBack().(func(response common.CallbackResponse)); ok {
err = client.AsyncRequest(&ivc, url, timeout, callBack, rest)
err = client.AsyncRequestContext(ctx, &ivc, url, timeout, callBack, rest)
res.SetError(err)
} else {
err = client.Send(&ivc, url, timeout)
err = client.SendContext(ctx, &ivc, url, timeout)
res.SetError(err)
}
} else {
if inv.Reply() == nil {
res.SetError(base.ErrNoReply)
} else {
err = client.Request(&ivc, url, timeout, rest)
err = client.RequestContext(ctx, &ivc, url, timeout, rest)
res.SetError(err)
}
}
Expand Down
12 changes: 11 additions & 1 deletion protocol/invocation/rpcinvocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
arguments []any
reply any
callBack any
ctx context.Context

Check warning on line 50 in protocol/invocation/rpcinvocation.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'context.Context' field and pass context as a parameter to methods that need it.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-yUvuioGPlgbDGtsVF&open=AZ-yUvuioGPlgbDGtsVF&pullRequest=3579
attachments map[string]any
// Refer to dubbo 2.7.6. It is different from attachment. It is used in internal process.
attributes map[string]any
Expand Down Expand Up @@ -267,13 +267,15 @@

func (r *RPCInvocation) GetAttachmentAsContext() context.Context {
ctx := r.Context()
Comment thread
XnLemon marked this conversation as resolved.
Resolved
var header = http.Header{}
header := cloneOutgoingHeader(triple_protocol.ExtractFromOutgoingContext(ctx))
for k, v := range r.Attachments() {
if str, ok := v.(string); ok {
header.Del(k)
header.Set(k, str)
continue
}
if str, ok := v.([]string); ok {
header.Del(k)
for _, s := range str {
header.Add(k, s)
}
Expand All @@ -283,6 +285,14 @@
return triple_protocol.NewOutgoingContext(ctx, header)
}

func cloneOutgoingHeader(header http.Header) http.Header {
cloned := make(http.Header, len(header))
for key, values := range header {
cloned[key] = append([]string(nil), values...)
}
return cloned
}

func (r *RPCInvocation) MergeAttachmentFromContext(ctx context.Context) {
header := triple_protocol.ExtractFromOutgoingContext(ctx)
if header == nil {
Expand Down
19 changes: 19 additions & 0 deletions protocol/invocation/rpcinvocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,25 @@ func TestRPCInvocation_GetAttachmentAsContextPreservesRequestContext(t *testing.
assert.Equal(t, "value", triple_protocol.ExtractFromOutgoingContext(ctx).Get("key"))
}

func TestRPCInvocation_GetAttachmentAsContextPreservesOutgoingAttachments(t *testing.T) {
requestCtx := context.Background()
requestCtx = triple_protocol.NewOutgoingContext(requestCtx, http.Header{
"Existing": {"existing-value"},
"Shared": {"old-value"},
})
invocation := NewRPCInvocationWithOptions(
WithContext(requestCtx),
WithAttachment("New", "new-value"),
WithAttachment("Shared", "new-shared-value"),
)

ctx := invocation.GetAttachmentAsContext()
header := triple_protocol.ExtractFromOutgoingContext(ctx)
assert.Equal(t, []string{"existing-value"}, header.Values("Existing"))
assert.Equal(t, []string{"new-value"}, header.Values("New"))
assert.Equal(t, []string{"new-shared-value"}, header.Values("Shared"))
}

func TestRPCInvocation_MergeAttachmentFromContext(t *testing.T) {
invocation := NewRPCInvocationWithOptions()

Expand Down
49 changes: 38 additions & 11 deletions protocol/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@
}

func (s *Server) handlePkg(conn net.Conn) {
connectionCtx, connectionCancel := context.WithCancel(context.Background())

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Defer the cancel function 'connectionCancel' after this context creation to prevent resource leaks.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-2EPyJkb2krUTIC4IH&open=AZ-2EPyJkb2krUTIC4IH&pullRequest=3579
writeConn := &lockedConn{Conn: conn}
var requestWG sync.WaitGroup
defer func() {
if r := recover(); r != nil {
logger.Warnf("[Jsonrpc][Server] connection panic, local=%v, remote=%v, err=%v, debug stack=%s",
conn.LocalAddr(), conn.RemoteAddr(), r, string(debug.Stack()))
}

connectionCancel()
conn.Close()
requestWG.Wait()
}()

setTimeout := func(conn net.Conn, timeout time.Duration) {
Expand Down Expand Up @@ -118,7 +123,7 @@
if err != nil {
return perrors.WithStack(err)
}
_, err = rspBuf.WriteTo(conn)
_, err = rspBuf.WriteTo(writeConn)
return perrors.WithStack(err)
}

Expand Down Expand Up @@ -162,32 +167,54 @@
return
}

requestCtx, requestCancel := context.WithCancel(connectionCtx)
r = r.WithContext(requestCtx)
ctx := contextFromRequest(r)
var timeoutCancel context.CancelFunc

if len(reqHeader["Timeout"]) > 0 {
timeout, err := time.ParseDuration(reqHeader["Timeout"])
if err == nil {
httpTimeout = timeout
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, httpTimeout)
defer cancel()
ctx, timeoutCancel = context.WithTimeout(ctx, httpTimeout)
}
delete(reqHeader, "Timeout")
}
setTimeout(conn, httpTimeout)

if err := serveRequest(ctx, reqHeader, reqBody, conn); err != nil {
if errRsp := sendErrorResp(r.Header, []byte(perrors.WithStack(err).Error())); errRsp != nil {
logger.Warnf("[Jsonrpc][Server] sendErrorResp failed, header=%v, err=%v, send_err=%v",
r.Header, perrors.WithStack(err), errRsp)
requestWG.Add(1)
go func(ctx context.Context, requestCancel, timeoutCancel context.CancelFunc, header map[string]string, body []byte, responseHeader http.Header) {
defer requestWG.Done()
defer requestCancel()
if timeoutCancel != nil {
defer timeoutCancel()
}

logger.Infof("[Jsonrpc][Server] unexpected error serving request, closing socket, err=%v", err)
return
}
if err := serveRequest(ctx, header, body, writeConn); err != nil {
if errRsp := sendErrorResp(responseHeader, []byte(perrors.WithStack(err).Error())); errRsp != nil {
logger.Warnf("[Jsonrpc][Server] sendErrorResp failed, header=%v, err=%v, send_err=%v",
responseHeader, perrors.WithStack(err), errRsp)
}

logger.Infof("[Jsonrpc][Server] unexpected error serving request, closing socket, err=%v", err)
connectionCancel()
conn.Close()
}
}(ctx, requestCancel, timeoutCancel, reqHeader, reqBody, r.Header)
}
}

type lockedConn struct {
net.Conn
writeMu sync.Mutex
}

func (c *lockedConn) Write(p []byte) (int, error) {
c.writeMu.Lock()
defer c.writeMu.Unlock()
return c.Conn.Write(p)
}

func contextFromRequest(r *http.Request) context.Context {
ctx := r.Context()
Comment thread
XnLemon marked this conversation as resolved.
Resolved
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
Expand Down
70 changes: 70 additions & 0 deletions protocol/jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package jsonrpc
import (
"bufio"
"context"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -32,6 +33,12 @@ import (
"github.com/stretchr/testify/require"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
"dubbo.apache.org/dubbo-go/v3/protocol/result"
)

// sendHTTPRequest writes an HTTP request to conn and returns the parsed response.
// A read deadline is set to avoid hanging when the server may not respond
// (e.g. valid content type but no registered service).
Expand Down Expand Up @@ -157,3 +164,66 @@ func TestHandlePkg_ContentType(t *testing.T) {
})
}
}

type blockingInvoker struct {
base.BaseInvoker
started chan struct{}
canceled chan struct{}
}

func (i *blockingInvoker) Invoke(ctx context.Context, _ base.Invocation) result.Result {
close(i.started)
<-ctx.Done()
close(i.canceled)
return &result.RPCResult{Err: ctx.Err()}
}

func TestHandlePkgCancelsInvocationWhenClientDisconnects(t *testing.T) {
const servicePath = "context-cancel-test"
protocol := GetProtocol().(*JsonrpcProtocol)
invoker := &blockingInvoker{
BaseInvoker: *base.NewBaseInvoker(common.NewURLWithOptions(
common.WithProtocol(JSONRPC),
common.WithPath("/"+servicePath),
)),
started: make(chan struct{}),
canceled: make(chan struct{}),
}
protocol.SetExporterMap(servicePath, NewJsonrpcExporter(servicePath, invoker, protocol.ExporterMap()))
t.Cleanup(func() { protocol.ExporterMap().Delete(servicePath) })

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

body := `{"jsonrpc":"2.0","method":"Blocked","params":[],"id":1}`
request := "POST /" + servicePath + " HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + fmt.Sprint(len(body)) + "\r\n\r\n" + body
_, err := clientConn.Write([]byte(request))
require.NoError(t, err)

select {
case <-invoker.started:
case <-time.After(time.Second):
t.Fatal("invoker was not started")
}
require.NoError(t, clientConn.Close())

select {
case <-invoker.canceled:
case <-time.After(time.Second):
t.Fatal("invocation context was not canceled after client disconnect")
}
select {
case <-handleDone:
case <-time.After(time.Second):
t.Fatal("connection handler did not exit")
}
require.NoError(t, serverConn.Close())
}
11 changes: 10 additions & 1 deletion registry/servicediscovery/service_discovery_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package servicediscovery

import (
"context"
"errors"
"math/rand/v2"
"sort"
Expand Down Expand Up @@ -59,6 +60,8 @@
// In order to keep compatible with interface-level registry,
// serviceDiscoveryRegistry = ServiceDiscovery + metadata
type serviceDiscoveryRegistry struct {
ctx context.Context

Check warning on line 63 in registry/servicediscovery/service_discovery_registry.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'context.Context' field and pass context as a parameter to methods that need it.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-2EP5Gkb2krUTIC4IJ&open=AZ-2EP5Gkb2krUTIC4IJ&pullRequest=3579
cancel context.CancelFunc
lock sync.RWMutex
url *common.URL
serviceDiscovery registry.ServiceDiscovery
Expand All @@ -76,7 +79,10 @@
if err != nil {
return nil, perrors.WithMessage(err, "Create service discovery failed")
}
ctx, cancel := context.WithCancel(context.Background())
return &serviceDiscoveryRegistry{
ctx: ctx,
cancel: cancel,
url: url,
serviceDiscovery: serviceDiscovery,
instanceURLs: make(map[registry.ServiceInstance]*common.URL),
Expand Down Expand Up @@ -322,6 +328,9 @@
}

func (s *serviceDiscoveryRegistry) Destroy() {
if s.cancel != nil {
s.cancel()
}
s.stopMetadataTimers()
err := s.serviceDiscovery.Destroy()
if err != nil {
Expand Down Expand Up @@ -542,7 +551,7 @@
// calls; holding the registry write lock across them would block every
// other subscribe/unsubscribe on this registry. The lock below only guards
// the serviceListeners check/install, never the external work.
listener := NewServiceInstancesChangedListener(url.GetParam(constant.ApplicationKey, ""), s.url.GetParam(constant.RegistryIdKey, constant.DefaultKey), services)
listener := NewServiceInstancesChangedListenerWithContext(s.ctx, url.GetParam(constant.ApplicationKey, ""), s.url.GetParam(constant.RegistryIdKey, constant.DefaultKey), services)
for _, serviceNameTmp := range services.Values() {
serviceName := serviceNameTmp.(string)
instances := s.serviceDiscovery.GetInstances(serviceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package servicediscovery

import (
"context"
"encoding/gob"
"reflect"
"sync"
Expand Down Expand Up @@ -59,6 +60,7 @@

// ServiceInstancesChangedListenerImpl The Service Discovery Changed Event Listener
type ServiceInstancesChangedListenerImpl struct {
ctx context.Context

Check warning on line 63 in registry/servicediscovery/service_instances_changed_listener_impl.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'context.Context' field and pass context as a parameter to methods that need it.

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-2EP5gkb2krUTIC4IL&open=AZ-2EP5gkb2krUTIC4IL&pullRequest=3579
app string
registryId string
serviceNames *gxset.HashSet
Expand All @@ -70,10 +72,20 @@
}

func NewServiceInstancesChangedListener(app string, registryId string, services *gxset.HashSet) registry.ServiceInstancesChangedListener {
return NewServiceInstancesChangedListenerWithContext(context.Background(), app, registryId, services)
}

// NewServiceInstancesChangedListenerWithContext creates a listener whose
// metadata refreshes are canceled with ctx, such as when its registry closes.
func NewServiceInstancesChangedListenerWithContext(ctx context.Context, app string, registryId string, services *gxset.HashSet) registry.ServiceInstancesChangedListener {
if ctx == nil {
ctx = context.Background()
}
cacheOnce.Do(func() {
initCache(app)
})
return &ServiceInstancesChangedListenerImpl{
ctx: ctx,
app: app,
registryId: registryId,
serviceNames: services,
Expand Down Expand Up @@ -128,7 +140,7 @@
revisionToInstances[key] = append(subInstances, instance)
metadataInfo := lstn.revisionToMetadata[key]
if metadataInfo == nil {
meta, err := GetMetadataInfo(providerApp, instance, revision, lstn.registryId)
meta, err := GetMetadataInfoWithContext(lstn.ctx, providerApp, instance, revision, lstn.registryId)
if err != nil {
// Skip this instance if metadata fetch fails (e.g., old Java Dubbo version)
// Try next instance with same revision
Expand Down Expand Up @@ -273,6 +285,15 @@
// and falls back to RPC if the report fails or returns nil. For all other storage
// types (including absent), it uses RPC directly.
func GetMetadataInfo(app string, instance registry.ServiceInstance, revision string, registryId string) (*info.MetadataInfo, error) {
return GetMetadataInfoWithContext(context.Background(), app, instance, revision, registryId)
}

// GetMetadataInfoWithContext retrieves metadata using the supplied lifecycle
// context for metadata RPC fallbacks.
func GetMetadataInfoWithContext(ctx context.Context, app string, instance registry.ServiceInstance, revision string, registryId string) (*info.MetadataInfo, error) {

Check failure on line 293 in registry/servicediscovery/service_instances_changed_listener_impl.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

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

See more on https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZ-2EP5gkb2krUTIC4IK&open=AZ-2EP5gkb2krUTIC4IK&pullRequest=3579
Comment thread
XnLemon marked this conversation as resolved.
Resolved
if ctx == nil {
ctx = context.Background()
}
cacheOnce.Do(func() {
initCache(app)
})
Expand Down Expand Up @@ -310,7 +331,7 @@
return metadataInfo, nil
}

metadataInfo, err = metadata.GetMetadataFromRpc(revision, instance)
metadataInfo, err = metadata.GetMetadataFromRpcWithContext(ctx, revision, instance)
if err != nil {
if reportErr != nil {
// Wrap rpcErr so callers can use errors.Is/As on the primary failure;
Expand All @@ -331,7 +352,7 @@
}

// Non-remote storage type ("local" or absent): fetch metadata via RPC directly.
metadataInfo, err = metadata.GetMetadataFromRpc(revision, instance)
metadataInfo, err = metadata.GetMetadataFromRpcWithContext(ctx, revision, instance)
if err != nil {
return nil, perrors.Wrapf(err,
"failed app=%s registry=%s revision=%s", app, registryId, revision)
Expand Down
Loading
Loading