From 26b8174f92ebe146bd90e8d96f2faebee02fcb9f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 27 Oct 2025 22:32:47 -0400 Subject: [PATCH] Fix METADAPT-A not ever closing the connection properly --- metadapta.go | 17 +++++++++++++---- metadapta_test.go | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/metadapta.go b/metadapta.go index 3b0ec61..c3b2575 100644 --- a/metadapta.go +++ b/metadapta.go @@ -6,6 +6,7 @@ import "fmt" import "net" import "sync" import "time" +import "context" import "sync/atomic" import "git.tebibyte.media/sashakoshka/go-util/sync" @@ -39,20 +40,23 @@ type a struct { sendLock sync.Mutex transMap map[int64] *transA transChan chan *transA - done chan struct { } + ctx context.Context + done func() err error } // AdaptA returns a connection implementing METADAPT-A over a singular stream- // oriented transport such as TCP or UNIX domain stream sockets. func AdaptA(underlying net.Conn, party Party) Conn { + ctx, done := context.WithCancel(context.Background()) conn := &a { sizeLimit: defaultSizeLimit, underlying: underlying, party: party, transMap: make(map[int64] *transA), transChan: make(chan *transA), - done: make(chan struct { }), + ctx: ctx, + done: done, } if party == ClientSide { conn.transID = 1 @@ -60,11 +64,15 @@ func AdaptA(underlying net.Conn, party Party) Conn { conn.transID = -1 } go conn.receive() + go func() { + <- ctx.Done() + underlying.Close() + }() return conn } func (this *a) Close() error { - close(this.done) + this.done() return nil } @@ -105,7 +113,7 @@ func (this *a) AcceptTrans() (Trans, error) { return nil, eof } return trans, nil - case <- this.done: + case <- this.ctx.Done(): return nil, eof } } @@ -490,6 +498,7 @@ func decodeMessageA( headerBuffer := [18]byte { } _, err = io.ReadFull(reader, headerBuffer[:]) if err != nil { return 0, 0, false, nil, err } + transID, err = decodeI64[int64](headerBuffer[:8]) if err != nil { return 0, 0, false, nil, err } method, err = decodeI16[uint16](headerBuffer[8:10]) diff --git a/metadapta_test.go b/metadapta_test.go index effc160..d88c586 100644 --- a/metadapta_test.go +++ b/metadapta_test.go @@ -52,6 +52,7 @@ func TestConnA(test *testing.T) { test.Error("CLIENT payload:", gotPayload) test.Fatal("CLIENT ok byeeeeeeeeeeeee") } + test.Log("CLIENT transaction has closed") } serverFunc := func(a Conn) {