internal/jsonrpc2_v2: rework Connection concurrency
This change fixes the semantics of Close to actually wait for in-flight requests before closing the ReadWriteCloser. (Previously, the Close method closed the ReadWriteCloser immediately, which I suspect is what led to many of the failures observed in golang/go#49387 and golang/go#46520.) It achieves this by explicitly tracking the number of in-flight requests, including requests with pending async responses, and explicitly rejecting new Call requests (while keeping the read loop open!) once Close has begun. To make it easier for me to reason about the request lifetimes, I reduced the number of long-lived goroutines from three to just one (the Read loop), with an additional Handler goroutine that runs only while the Handler queue is non-empty. Now, it is clearer (I hope!) that the number of in-flight async requests strictly decreases after Close has begun, even though the Read goroutine continues to read requests (and, importantly, responses) and to forward Notifications to the preempter. For golang/go#49387 For golang/go#46520 Change-Id: Idf5960f848108a7ced78c5382099c8692e9b181e Reviewed-on: https://go-review.googlesource.com/c/tools/+/388134 gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
5d6e2930bf
commit
1afb0eb5ac
1 changed files with 621 additions and 349 deletions
|
|
@ -9,7 +9,6 @@ import (
|
|||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// This file contains implementations of the transport primitives that use the standard network
|
||||
|
|
@ -36,7 +35,7 @@ type netListener struct {
|
|||
}
|
||||
|
||||
// Accept blocks waiting for an incoming connection to the listener.
|
||||
func (l *netListener) Accept(ctx context.Context) (io.ReadWriteCloser, error) {
|
||||
func (l *netListener) Accept(context.Context) (io.ReadWriteCloser, error) {
|
||||
return l.net.Accept()
|
||||
}
|
||||
|
||||
|
|
@ -56,9 +55,7 @@ func (l *netListener) Close() error {
|
|||
|
||||
// Dialer returns a dialer that can be used to connect to the listener.
|
||||
func (l *netListener) Dialer() Dialer {
|
||||
return NetDialer(l.net.Addr().Network(), l.net.Addr().String(), net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
})
|
||||
return NetDialer(l.net.Addr().Network(), l.net.Addr().String(), net.Dialer{})
|
||||
}
|
||||
|
||||
// NetDialer returns a Dialer using the supplied standard network dialer.
|
||||
|
|
@ -98,15 +95,19 @@ type netPiper struct {
|
|||
}
|
||||
|
||||
// Accept blocks waiting for an incoming connection to the listener.
|
||||
func (l *netPiper) Accept(ctx context.Context) (io.ReadWriteCloser, error) {
|
||||
// block until we have a listener, or are closed or cancelled
|
||||
func (l *netPiper) Accept(context.Context) (io.ReadWriteCloser, error) {
|
||||
// Block until the pipe is dialed or the listener is closed,
|
||||
// preferring the latter if already closed at the start of Accept.
|
||||
select {
|
||||
case <-l.done:
|
||||
return nil, errClosed
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case rwc := <-l.dialed:
|
||||
return rwc, nil
|
||||
case <-l.done:
|
||||
return nil, io.EOF
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
return nil, errClosed
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -124,6 +125,14 @@ func (l *netPiper) Dialer() Dialer {
|
|||
|
||||
func (l *netPiper) Dial(ctx context.Context) (io.ReadWriteCloser, error) {
|
||||
client, server := net.Pipe()
|
||||
l.dialed <- server
|
||||
return client, nil
|
||||
|
||||
select {
|
||||
case l.dialed <- server:
|
||||
return client, nil
|
||||
|
||||
case <-l.done:
|
||||
client.Close()
|
||||
server.Close()
|
||||
return nil, errClosed
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue