Skip to content

Commit 65de96c

Browse files
authored
fix: Leaking yamux session after HTTP handler is closed (#329)
* fix: Leaking yamux session after HTTP handler is closed Closes #317. The httptest server cancels the context after the connection is closed, but if a connection takes a long time to close, the request would never end. This applies a context to the entire listener that cancels on test cleanup. After discussion with @bryphe-coder, reducing the parallel limit on Windows is likely to reduce failures as well. * Switch to windows-2022 to improve decompression * Invalidate cache on matrix OS
1 parent f7b4849 commit 65de96c

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

.github/workflows/coder.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
os:
123123
- ubuntu-latest
124124
- macos-latest
125-
- windows-latest
125+
- windows-2022
126126
steps:
127127
- uses: actions/checkout@v2
128128

@@ -138,9 +138,9 @@ jobs:
138138
~/.cache/go-build
139139
~/Library/Caches/go-build
140140
%LocalAppData%\go-build
141-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
141+
key: ${{ matrix.os }}-go-${{ hashFiles('**/go.sum') }}
142142
restore-keys: |
143-
${{ runner.os }}-go-
143+
${{ matrix.os }}-go-
144144
145145
- run: go install gotest.tools/gotestsum@latest
146146

@@ -150,9 +150,13 @@ jobs:
150150
terraform_wrapper: false
151151

152152
- name: Test with Mock Database
153+
shell: bash
154+
env:
155+
GOCOUNT: ${{ runner.os == 'Windows' && 3 || 5 }}
156+
GOMAXPROCS: ${{ runner.os == 'Windows' && 1 || 2 }}
153157
run: gotestsum --junitfile="gotests.xml" --packages="./..." --
154158
-covermode=atomic -coverprofile="gotests.coverage"
155-
-timeout=3m -count=5 -race -short -parallel=2
159+
-timeout=3m -count=$GOCOUNT -race -short -failfast
156160

157161
- name: Upload DataDog Trace
158162
if: (success() || failure()) && github.actor != 'dependabot[bot]'
@@ -166,10 +170,10 @@ jobs:
166170
if: runner.os == 'Linux'
167171
run: DB=true gotestsum --junitfile="gotests.xml" --packages="./..." --
168172
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
169-
-count=1 -race -parallel=2
173+
-count=1 -race -parallel=2 -failfast
170174

171175
- name: Upload DataDog Trace
172-
if: (success() || failure()) && github.actor != 'dependabot[bot]'
176+
if: (success() || failure()) && github.actor != 'dependabot[bot]' && runner.os == 'Linux'
173177
env:
174178
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
175179
DD_DATABASE: postgresql

coderd/coderdtest/coderdtest.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"io"
7+
"net"
78
"net/http/httptest"
89
"net/url"
910
"os"
@@ -59,7 +60,13 @@ func New(t *testing.T) *codersdk.Client {
5960
Database: db,
6061
Pubsub: pubsub,
6162
})
62-
srv := httptest.NewServer(handler)
63+
srv := httptest.NewUnstartedServer(handler)
64+
srv.Config.BaseContext = func(_ net.Listener) context.Context {
65+
ctx, cancelFunc := context.WithCancel(context.Background())
66+
t.Cleanup(cancelFunc)
67+
return ctx
68+
}
69+
srv.Start()
6370
serverURL, err := url.Parse(srv.URL)
6471
require.NoError(t, err)
6572
t.Cleanup(srv.Close)

peer/conn.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ func (c *Conn) init() error {
183183
}
184184
})
185185
c.rtc.OnConnectionStateChange(func(peerConnectionState webrtc.PeerConnectionState) {
186-
if c.isClosed() {
187-
// Make sure we don't log after Close() has been called.
188-
return
189-
}
190-
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
191-
slog.F("state", peerConnectionState))
186+
go func() {
187+
c.closeMutex.Lock()
188+
defer c.closeMutex.Unlock()
189+
if c.isClosed() {
190+
return
191+
}
192+
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
193+
slog.F("state", peerConnectionState))
194+
}()
192195

193196
switch peerConnectionState {
194197
case webrtc.PeerConnectionStateDisconnected:

provisionerd/provisionerd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ func (p *provisionerDaemon) connect(ctx context.Context) {
110110
if errors.Is(err, context.Canceled) {
111111
return
112112
}
113+
p.closeMutex.Lock()
113114
if p.isClosed() {
115+
p.closeMutex.Unlock()
114116
return
115117
}
116118
p.opts.Logger.Warn(context.Background(), "failed to dial", slog.Error(err))
119+
p.closeMutex.Unlock()
117120
continue
118121
}
119122
p.opts.Logger.Debug(context.Background(), "connected")

pty/pty_other.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package pty
66
import (
77
"io"
88
"os"
9+
"sync"
910

1011
"github.com/creack/pty"
1112
)
@@ -23,6 +24,7 @@ func newPty() (PTY, error) {
2324
}
2425

2526
type otherPty struct {
27+
mutex sync.Mutex
2628
pty, tty *os.File
2729
}
2830

@@ -41,13 +43,18 @@ func (p *otherPty) Output() io.ReadWriter {
4143
}
4244

4345
func (p *otherPty) Resize(cols uint16, rows uint16) error {
46+
p.mutex.Lock()
47+
defer p.mutex.Unlock()
4448
return pty.Setsize(p.tty, &pty.Winsize{
4549
Rows: rows,
4650
Cols: cols,
4751
})
4852
}
4953

5054
func (p *otherPty) Close() error {
55+
p.mutex.Lock()
56+
defer p.mutex.Unlock()
57+
5158
err := p.pty.Close()
5259
if err != nil {
5360
return err

pty/start_other.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88
"syscall"
99

1010
"github.com/creack/pty"
11+
"golang.org/x/xerrors"
1112
)
1213

1314
func startPty(cmd *exec.Cmd) (PTY, error) {
1415
ptty, tty, err := pty.Open()
1516
if err != nil {
16-
return nil, err
17+
return nil, xerrors.Errorf("open: %w", err)
1718
}
19+
defer func() {
20+
_ = tty.Close()
21+
}()
1822
cmd.SysProcAttr = &syscall.SysProcAttr{
1923
Setsid: true,
2024
Setctty: true,
@@ -25,7 +29,7 @@ func startPty(cmd *exec.Cmd) (PTY, error) {
2529
err = cmd.Start()
2630
if err != nil {
2731
_ = ptty.Close()
28-
return nil, err
32+
return nil, xerrors.Errorf("start: %w", err)
2933
}
3034
return &otherPty{
3135
pty: ptty,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy