SQLite Forum

Tcl coroutine, [db eval ... script], yield, segmentation fault
Login

Tcl coroutine, [db eval ... script], yield, segmentation fault

(1) By Poor Yorick (pooryorick) on 2020-04-09 21:15:05 [link] [source]

The following script produces a segmentation fault:

#! /usr/bin/env tclsh

package require sqlite3
proc p {} {
    set statement {select * from t}
    db eval $statement {
        yield
    }
}
sqlite3 db :memory:
db eval {
    create table t (v)
    ; insert into t(v) values (1) ,(2)
}

coroutine p1 p 
p1
rename db {}
p1

With a Tcl debugging build the segmentation fault happens at a more descriptive spot. The SqliteDb structure is released then the database routine is deleted, but then later dbReleaseStmt() attempts to access that structure.

(2) By Andreas Kupries (andreas-kupries) on 2020-04-09 21:37:22 in reply to 1 [link] [source]

Which version of sqlite3 (and associated tcl binding) is that ?

I know that the 3.25.3 I currently use would error on the yield with C stack busy. I.e. that Tcl binding does not properly use the Tcl8.6+ non-recursive engine APIs.

(3) By Poor Yorick (pooryorick) on 2020-04-09 21:43:40 in reply to 2 [source]

This is on trunk. There has been some work in the last few years on NRE-enabling the Tcl binding.

There is also a related invalid memory issue when deleting a coroutine that is in the middle of a sqlite transaction. Any fix for this issue is likely to fix that as well.

(4.1) By Andreas Kupries (andreas-kupries) on 2020-04-09 22:08:55 edited from 4.0 in reply to 3 [link] [source]

Thanks. Time to update the local sqlite installation (I am using a chunked SELECT with LIMIT in a personal project because yield did not work. Should have checked versions first).

(5) By Andreas Kupries (andreas-kupries) on 2020-04-16 21:59:27 in reply to 4.1 [link] [source]

I suspect that my non-working 3.25.3 was compiled against Tcl 8.5, which does not have NRE. The 3.32.0 I compiled recently against 8.6 definitely works. And an older 3.20.1 I found installed also works.

Bisecting for when the NRE support was added tells me 2009-10-06 14:59, via commit e9f72f1de4, which went into release 3.6.19.