Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1028)

Issue 5759050: code review 5759050: database/sql: ensure Stmts are correctly closed. (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
13 years ago by gwenn
Modified:
13 years ago
Reviewers:
CC:
golang-dev, bradfitz
Visibility:
Public.

Description

database/sql: ensure Stmts are correctly closed. To make sure that there is no resource leak, I suggest to fix the 'fakedb' driver such as it fails when any Stmt is not closed. First, add a check in fakeConn.Close(). Then, fix all missing Stmt.Close()/Rows.Close(). I am not sure that the strategy choose in fakeConn.Prepare/prepare* is ok. The weak point in this patch is the change in Tx.Query: - Tests pass without this change, - I found it by manually analyzing the code, - I just try to make Tx.Query look like DB.Query.

Patch Set 1 #

Patch Set 2 : diff -r f4470a54e6db https://go.googlecode.com/hg #

Patch Set 3 : diff -r c1f5756f94b0 https://go.googlecode.com/hg #

Patch Set 4 : diff -r 885465912b99 https://go.googlecode.com/hg #

Patch Set 5 : diff -r 1f5208fb59ff https://go.googlecode.com/hg #

Unified diffs Side-by-side diffs Delta from patch set Stats (+28 lines, -4 lines) Patch
M src/pkg/database/sql/fakedb_test.go View 1 2 3 4 7 chunks +15 lines, -0 lines 0 comments Download
M src/pkg/database/sql/sql.go View 1 2 3 4 2 chunks +5 lines, -3 lines 0 comments Download
M src/pkg/database/sql/sql_test.go View 1 2 3 4 5 chunks +8 lines, -1 line 0 comments Download

Messages

Total messages: 15
gwenn
Hello golang-dev@googlegroups.com, I'd like you to review this change to https://go.googlecode.com/hg
13 years ago (2012-03-06 08:12:34 UTC) #1
bradfitz
Gwenn, This patch looks fine to me, but just so I understand: does this fix ...
13 years ago (2012-03-06 23:00:43 UTC) #2
bradfitz
Gwenn, In particular, I tried to add your sqlite driver (github.com/gwenn/gosqlite) to the external go-sql-test ...
13 years ago (2012-03-06 23:16:55 UTC) #3
gwenn
Brad, You are right when you say that this patch fix only a minor bug. ...
13 years ago (2012-03-07 12:58:30 UTC) #4
gwenn
Brad, As specified on github, 1) the build error is related to your sqlite version, ...
13 years ago (2012-03-07 13:19:58 UTC) #5
gwenn
Hello golang-dev@googlegroups.com, bradfitz@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
13 years ago (2012-03-10 10:20:27 UTC) #6
gwenn
A new test 'TestTxQueryInvalid' has been added to reproduce the minor bug in Tx.Query: "error ...
13 years ago (2012-03-10 10:30:28 UTC) #7
gwenn
Ok, The same connection is released (ie putConn) twice: 1) by tx.Query() -> Stmt.Query() with ...
13 years ago (2012-03-10 19:29:41 UTC) #8
bradfitz
Yes, see the patch I just submitted with the fix. Could you update this patch ...
13 years ago (2012-03-10 19:33:28 UTC) #9
gwenn
Hello golang-dev@googlegroups.com, bradfitz@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
13 years ago (2012-03-10 20:18:26 UTC) #10
gwenn
Nice, Your fix make all tests pass. I have merged/updated the patch. Thanks. On Sat, ...
13 years ago (2012-03-10 20:21:07 UTC) #11
bradfitz
[+mattn.jp] Mattn's sqlite driver still fails my tests, though: ante:sqltest $ git remote -v origin ...
13 years ago (2012-03-10 22:13:12 UTC) #12
bradfitz
LGTM On Sat, Mar 10, 2012 at 12:21 PM, gwenn <gwenn.kahz@gmail.com> wrote: > Nice, > ...
13 years ago (2012-03-10 23:18:58 UTC) #13
bradfitz
*** Submitted as http://code.google.com/p/go/source/detail?r=91a86970157c *** database/sql: ensure Stmts are correctly closed. To make sure that ...
13 years ago (2012-03-10 23:25:14 UTC) #14
gwenn
13 years ago (2012-03-11 11:34:09 UTC) #15
Hello,
First, I think that Rows.Close() implementation should be fixed: the
associated Stmt must be Reset (and not Closed).

But with this fix, the go-sql-test still fails:
2012/03/11 12:23:14 SQLITE: Library used incorrectly, Dangling
statement (not finalize): "INSERT INTO t (count) VALUES (?)"
...
2012/03/11 12:23:14 SQLITE: Library used incorrectly, Dangling
statement (not finalize): "SELECT count FROM t ORDER BY count DESC"
...
2012/03/11 12:23:14 SQLITE: Library used incorrectly, Dangling
statement (not finalize): ""
panic: runtime error: invalid memory address or nil pointer dereference
...
I am investigating.
But I am stupid/slow: how do you easily have a backtrace in Go?
Regards.

On Sat, Mar 10, 2012 at 11:13 PM, Brad Fitzpatrick <bradfitz@golang.org> wrote:
> [+mattn.jp]
>
> Mattn's sqlite driver still fails my tests, though:
>
> ante:sqltest $ git remote -v
> origin git@github.com:bradfitz/go-sql-test.git (fetch)
> origin git@github.com:bradfitz/go-sql-test.git (push)
>
> ante:sqltest $ pwd
> /home/bradfitz/hack/go-sql-test/src/sqltest
>
> ante:sqltest $ echo $GOPATH
> /home/bradfitz/hack/go-sql-test
>
> ante:sqltest $ go test -v -run=TestPreparedStmt_SQLite
>
> === RUN TestPreparedStmt_SQLite
> panic: runtime error: invalid memory address or nil pointer dereference
> [signal 0xb code=0x1 addr=0x2a pc=0x7f7e6f590840]
>
> goroutine 1 [chan receive]:
> testing.RunTests(0x400c00, 0x682070, 0xc0000000c, 0x1, 0xf800000007, ...)
> /home/bradfitz/go/src/pkg/testing/testing.go:350 +0x79f
> testing.Main(0x400c00, 0x682070, 0xc0000000c, 0x685480, 0x0, ...)
> /home/bradfitz/go/src/pkg/testing/testing.go:285 +0x7a
> main.main()
> /tmp/go-build762216293/sqltest/_test/_testmain.go:65 +0x91
>
> goroutine 2 [syscall]:
> created by runtime.main
> /home/bradfitz/go/src/pkg/runtime/proc.c:221
>
> goroutine 3 [syscall]:
> github.com/mattn/go-sqlite3._Cfunc_sqlite3_reset(0x3d07188, 0xf840092100)
> /tmp/go-build762216293/github.com/mattn/go-sqlite3/_obj/_cgo_defun.c:218
> +0x2f
> github.com/mattn/go-sqlite3.(*SQLiteStmt).bind(0xf840057c90, 0x6856e0, 0x0,
> 0x0, 0x0, ...)
> /tmp/go-build762216293/github.com/mattn/go-sqlite3/_obj/sqlite3.cgo1.go:170
> +0x34
> github.com/mattn/go-sqlite3.(*SQLiteStmt).Query(0xf840057c90, 0x6856e0, 0x0,
> 0x0, 0x0, ...)
> /tmp/go-build762216293/github.com/mattn/go-sqlite3/_obj/sqlite3.cgo1.go:223
> +0x4a
> database/sql.(*Stmt).Query(0xf8400588a0, 0x0, 0x0, 0x8, 0x100000001, ...)
> /home/bradfitz/go/src/pkg/database/sql/sql.go:812 +0x28e
> database/sql.(*Stmt).QueryRow(0xf8400588a0, 0x0, 0x0, 0xf840057c30,
> 0xf840063760, ...)
> /home/bradfitz/go/src/pkg/database/sql/sql.go:840 +0x40
> sqltest._func_004(0xf8400711a8, 0x7f7e6f995da8, 0x7f7e6f995e38,
> 0x7f7e6f995de8, 0xf840063700, ...)
> /home/bradfitz/hack/go-sql-test/src/sqltest/sql_test.go:301 +0x8e
> sqltest.testPreparedStmt(0xf840057990, 0xf8400579c0)
> /home/bradfitz/hack/go-sql-test/src/sqltest/sql_test.go:310 +0x4db
> sqltest.sqliteDB.RunTest(0xf840064230, 0x426e5a, 0x426dc3, 0xf8400579c0)
> /home/bradfitz/hack/go-sql-test/src/sqltest/sql_test.go:132 +0x361
> sqltest.sqliteDB.RunTest·i(0xf8400579c0, 0xf840064230, 0x426e5a, 0x421b31,
> 0xf840064230, ...)
> /home/bradfitz/hack/go-sql-test/src/sqltest/drivers.go:0 +0x38
> sqltest.TestPreparedStmt_SQLite(0xf840064230, 0x1e965668)
> /home/bradfitz/hack/go-sql-test/src/sqltest/sql_test.go:271 +0x44
> testing.tRunner(0xf840064230, 0x682148, 0x0, 0x0)
> /home/bradfitz/go/src/pkg/testing/testing.go:273 +0x6f
> created by testing.RunTests
> /home/bradfitz/go/src/pkg/testing/testing.go:349 +0x77c
> exit status 2
> FAIL sqltest 0.022s
>
> Or, sometimes it fails like:
>
> --- FAIL: TestPreparedStmt_SQLite (0.02 seconds)
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:302: Query: sql: statement expects 1117 inputs; got 0
>
>
> Or sometimes like:
>
> --- FAIL: TestPreparedStmt_SQLite (0.05 seconds)
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:306: Insert: database is locked
> sql_test.go:302: Query: SQL logic error or missing database
> FAIL
>
>
> I could use help debugging why the mattn driver fails like this.  I'll keep
> digging, but more eyeballs welcome.
>
> - Brad
>
>
> On Sat, Mar 10, 2012 at 12:21 PM, gwenn <gwenn.kahz@gmail.com> wrote:
>>
>> Nice,
>> Your fix make all tests pass.
>> I have merged/updated the patch.
>> Thanks.
>>
>> On Sat, Mar 10, 2012 at 8:33 PM, Brad Fitzpatrick <bradfitz@golang.org>
>> wrote:
>> > Yes, see the patch I just submitted with the fix.
>> >
>> > Could you update this patch to apply to tip?
>> >
>> > On Mar 10, 2012 11:29 AM, <gwenn.kahz@gmail.com> wrote:
>> >>
>> >> Ok,
>> >> The same connection is released (ie putConn) twice:
>> >> 1) by tx.Query() -> Stmt.Query() with err,
>> >> 2) by defer tx.Rollback().
>> >> If I comment out the "s.db.putConn(ci, err)", all tests pass but it
>> >> seems bad.
>> >>
>> >> http://codereview.appspot.com/5759050/
>
>
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b