-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition with SQLite3Store.startCleanup #228
Comments
Here is a test that fails under diff --git a/sqlite3store/sqlite3store_test.go b/sqlite3store/sqlite3store_test.go
index 71908a5..dd809f1 100644
--- a/sqlite3store/sqlite3store_test.go
+++ b/sqlite3store/sqlite3store_test.go
@@ -269,7 +269,6 @@ func TestCleanup(t *testing.T) {
}
p := NewWithCleanupInterval(db, 200*time.Millisecond)
- defer p.StopCleanup()
err = p.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond))
if err != nil {
@@ -287,6 +286,8 @@ func TestCleanup(t *testing.T) {
}
time.Sleep(300 * time.Millisecond)
+ p.StopCleanup()
+
row = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'")
err = row.Scan(&count)
if err != nil {
The above change fixes the race: diff --git a/sqlite3store/sqlite3store.go b/sqlite3store/sqlite3store.go
index ff2860e..7b05ff0 100644
--- a/sqlite3store/sqlite3store.go
+++ b/sqlite3store/sqlite3store.go
@@ -25,6 +25,7 @@ func New(db *sql.DB) *SQLite3Store {
func NewWithCleanupInterval(db *sql.DB, cleanupInterval time.Duration) *SQLite3Store {
p := &SQLite3Store{db: db}
if cleanupInterval > 0 {
+ p.stopCleanup = make(chan bool)
go p.startCleanup(cleanupInterval)
}
return p
@@ -96,7 +97,6 @@ func (p *SQLite3Store) All() (map[string][]byte, error) {
}
func (p *SQLite3Store) startCleanup(interval time.Duration) {
- p.stopCleanup = make(chan bool)
ticker := time.NewTicker(interval)
for {
select { |
This was referenced Dec 20, 2024
earthboundkid
added a commit
to spotlightpa/moreofa
that referenced
this issue
Dec 21, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My tests hit a race. IIUC, the cause of the race is that there is a bug in SQLite3Store.
Notice
p.startCleanup(cleanupInterval)
is called withgo
.Observe that
p.stopCleanup
is written to inside that goroutine, without any lock onp
.Finally
The line
p.stopCleanup <- true
is good and race free, butp.stopCleanup != nil
is a race because it is readingstopCleanup
, which was written to in thestartCleanup
routine.I think the solution is to change
NewWithCleanupInterval
toAnd remove
p.stopCleanup = make(chan bool)
fromstartCleanup
.The text was updated successfully, but these errors were encountered: