Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e9fe62

Browse files
committed
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.
1 parent 5ef59e7 commit 4e9fe62

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

.github/workflows/coder.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

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)