go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

In concurrent scenarios, API calls to RPC may result in context cancel

Open z406497819 opened this issue 1 year ago • 4 comments

Describe the bug Here we used the example of API calling RPC in the greet demo, The implementation logic is very simple, it just outputs hello world At first, I thought it was a version issue. Go Zero upgraded from 1.6.0 to 1.6.5 and then to 1.7.0, but this problem still occurs The higher the concurrency, the greater the probability of occurrence At concurrency of 100, it is almost inevitable The only change made was sleeping for 5 seconds in the RPC layer

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The code is

rpc:

Name: car.rpc
ListenOn: 0.0.0.0:8081
Timeout: 60000
// logic
func (l *PingLogic) Ping(in *car.Req) (*car.Resp, error) {

	time.Sleep(time.Second*5)

	return &car.Resp{
		Pong: "hello",
	}, nil
}

api:

Name: car
Host: 0.0.0.0
Port: 8889
Timeout: 60000

CarRpc:
  Timeout: 60000
  Endpoints:
    - 192.168.1.3:8081
func (l *TestLogic) Test() (resp *types.Resp, err error) {

	res, err := l.svcCtx.CarRpc.Ping(l.ctx, &car.Req{})
	if err != nil {
		return
	}

	return &types.Resp{
		Name: res.Pong,
	}, nil
}

  1. The error is

http response status:408 rpc error: code = Canceled desc = context canceled



**Screenshots**
![image](https://github.com/user-attachments/assets/30d73fc9-6e07-4555-bbc9-f4208f35d5d1)

**Environments (please complete the following information):**
- OS: linux
- go version 1.22.1
- go-zero version 1.7.0
- goctl version 1.7.0

Hope to solve this problem, thank you everyone

z406497819 avatar Aug 07 '24 08:08 z406497819

Looking forward to resolving 🙏

z406497819 avatar Aug 07 '24 08:08 z406497819

I have this problem too

evgeniy-dammer avatar Aug 07 '24 11:08 evgeniy-dammer

Is there a complete example code? I can't reproduce it according to the above description.

NicoleRobin avatar Aug 26 '24 13:08 NicoleRobin

context canceled means clients closed the requests. Typically, the scenario is the user closes the browser window before the server responds.

kevwan avatar Aug 27 '24 11:08 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 28 '25 02:08 github-actions[bot]