From 1b2c267b2bf378328b28fc7170568f11ffd98657 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 16 Jan 2024 15:08:26 +0000 Subject: [PATCH] Optimize interrupts. --- conn.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/conn.go b/conn.go index 255e5b0..93d6385 100644 --- a/conn.go +++ b/conn.go @@ -244,18 +244,18 @@ func (c *Conn) SetInterrupt(ctx context.Context) (old context.Context) { // A busy SQL statement prevents SQLite from ignoring an interrupt // that comes before any other statements are started. if c.pending == nil { - c.pending, _, _ = c.Prepare(`SELECT 1 UNION ALL SELECT 2`) - } else { - c.pending.Reset() + c.pending, _, _ = c.Prepare(`WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x FROM c) SELECT x FROM c`) } old = c.interrupt c.interrupt = ctx - if ctx == nil || ctx.Done() == nil { - return old - } - c.pending.Step() + if old != nil && old.Done() != nil && (ctx == nil || ctx.Err() == nil) { + c.pending.Reset() + } + if ctx != nil && ctx.Done() != nil { + c.pending.Step() + } return old }