Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ext/sqlite3/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,10 @@ rb_sqlite3_statement_timeout(void *context)
clock_gettime(CLOCK_MONOTONIC, &currentTime);

if (!timespecisset(&ctx->stmt_deadline)) {
// Set stmt_deadline if not already set
ctx->stmt_deadline = currentTime;
struct timespec timeout;
timeout.tv_sec = ctx->stmt_timeout / 1000;
timeout.tv_nsec = (ctx->stmt_timeout % 1000) * 1000000L;
timespecadd(&currentTime, &timeout, &ctx->stmt_deadline);
} else if (timespecafter(&currentTime, &ctx->stmt_deadline)) {
return 1;
}
Expand Down
9 changes: 9 additions & 0 deletions ext/sqlite3/timespec.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
(vsp)->tv_nsec += 1000000000L; \
} \
} while (0)
#define timespecadd(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
if ((vsp)->tv_nsec >= 1000000000L) { \
(vsp)->tv_sec++; \
(vsp)->tv_nsec -= 1000000000L; \
} \
} while (0)
#define timespecafter(tsp, usp) \
(((tsp)->tv_sec > (usp)->tv_sec) || \
((tsp)->tv_sec == (usp)->tv_sec && (tsp)->tv_nsec > (usp)->tv_nsec))
10 changes: 10 additions & 0 deletions test/test_integration_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ def test_long_running_statements_get_interrupted_when_statement_timeout_set
SELECT i FROM r ORDER BY i LIMIT 1;
SQL
end

# (regression) Ensure queries that finish within the timeout complete,
# even ones running many instructions (time is instructions-based, not wall call)
@db.statement_timeout = 1_000
result = @db.execute <<~SQL
WITH RECURSIVE r(i) AS (VALUES(0) UNION ALL SELECT i+1 FROM r LIMIT 100000)
SELECT count(i) FROM r;
SQL
assert_equal 100_000, result.first.first
ensure
@db.statement_timeout = 0
end
end