Replying to Maxim Dounin:
The loop is expected to stop once the u->buffer is full. Could you please clarify the configuration you are seeing the problem with?
First of all, u->buffer is not full in my scenario. And n = upstream->recv(upstream, b->last, size); Always return -1.
Because the nginx production environment configuration is very complicated, it cannot be provided. And this phenomenon is not easy to repeat, only occasionally.
But I was sure that size = b->end - b->last; the size was not 0 at that time. I gdb checked the scene.
I still believe this problem is related to n = ngx_writev(c, &header); returning -2.