Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita82c0eb

Browse files
authored
Fix socket leak, clean up single use postgres databases (#2413)
* Fix socket leak, clean up single use postgres databasesSigned-off-by: Spike Curtis <spike@coder.com>* Move migrate close defer until after we know it is not nilSigned-off-by: Spike Curtis <spike@coder.com>
1 parenteab5c15 commita82c0eb

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

‎Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ test: test-clean
115115
.PHONY: test-postgres
116116
test-postgres: test-clean
117117
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." --\
118-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=10m\
118+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m\
119119
-coverpkg=./...,github.com/coder/coder/codersdk\
120-
-count=1 -parallel=1 -race -failfast
120+
-count=1 -race -failfast
121121

122122

123123
.PHONY: test-postgres-docker

‎cli/list_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/stretchr/testify/require"
8+
"github.com/stretchr/testify/assert"
99

1010
"github.com/coder/coder/cli/clitest"
1111
"github.com/coder/coder/coderd/coderdtest"
@@ -30,13 +30,15 @@ func TestList(t *testing.T) {
3030
pty:=ptytest.New(t)
3131
cmd.SetIn(pty.Input())
3232
cmd.SetOut(pty.Output())
33-
errC:=make(chanerror)
33+
done:=make(chanany)
3434
gofunc() {
35-
errC<-cmd.ExecuteContext(ctx)
35+
errC:=cmd.ExecuteContext(ctx)
36+
assert.NoError(t,errC)
37+
close(done)
3638
}()
3739
pty.ExpectMatch(workspace.Name)
3840
pty.ExpectMatch("Running")
3941
cancelFunc()
40-
require.NoError(t,<-errC)
42+
<-done
4143
})
4244
}

‎coderd/database/migrate.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package database
22

33
import (
4+
"context"
45
"database/sql"
56
"embed"
67
"errors"
@@ -17,12 +18,21 @@ import (
1718
varmigrations embed.FS
1819

1920
funcmigrateSetup(db*sql.DB) (source.Driver,*migrate.Migrate,error) {
21+
ctx:=context.Background()
2022
sourceDriver,err:=iofs.New(migrations,"migrations")
2123
iferr!=nil {
2224
returnnil,nil,xerrors.Errorf("create iofs: %w",err)
2325
}
2426

25-
dbDriver,err:=postgres.WithInstance(db,&postgres.Config{})
27+
// there is a postgres.WithInstance() method that takes the DB instance,
28+
// but, when you close the resulting Migrate, it closes the DB, which
29+
// we don't want. Instead, create just a connection that will get closed
30+
// when migration is done.
31+
conn,err:=db.Conn(ctx)
32+
iferr!=nil {
33+
returnnil,nil,xerrors.Errorf("postgres connection: %w",err)
34+
}
35+
dbDriver,err:=postgres.WithConnection(ctx,conn,&postgres.Config{})
2636
iferr!=nil {
2737
returnnil,nil,xerrors.Errorf("wrap postgres connection: %w",err)
2838
}
@@ -36,11 +46,22 @@ func migrateSetup(db *sql.DB) (source.Driver, *migrate.Migrate, error) {
3646
}
3747

3848
// MigrateUp runs SQL migrations to ensure the database schema is up-to-date.
39-
funcMigrateUp(db*sql.DB)error {
49+
funcMigrateUp(db*sql.DB)(retErrerror) {
4050
_,m,err:=migrateSetup(db)
4151
iferr!=nil {
4252
returnxerrors.Errorf("migrate setup: %w",err)
4353
}
54+
deferfunc() {
55+
srcErr,dbErr:=m.Close()
56+
ifretErr!=nil {
57+
return
58+
}
59+
ifdbErr!=nil {
60+
retErr=dbErr
61+
return
62+
}
63+
retErr=srcErr
64+
}()
4465

4566
err=m.Up()
4667
iferr!=nil {

‎coderd/database/postgres/postgres.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ func Open() (string, func(), error) {
4444
return"",nil,xerrors.Errorf("create db: %w",err)
4545
}
4646

47-
return"postgres://postgres:postgres@127.0.0.1:5432/"+dbName+"?sslmode=disable",func() {},nil
47+
deleteDB:=func() {
48+
ddb,_:=sql.Open("postgres",dbURL)
49+
deferddb.Close()
50+
_,_=ddb.Exec("DROP DATABASE "+dbName)
51+
}
52+
53+
return"postgres://postgres:postgres@127.0.0.1:5432/"+dbName+"?sslmode=disable",deleteDB,nil
4854
}
4955

5056
pool,err:=dockertest.NewPool("")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp