Skip to content

Commit 91f46f7

Browse files
Fix doomed transaction error handling in delta framework and ensure_collection_table (#756) (#757)
When calculate_deltas was called inside a collector's transaction and failed, the CATCH block tried to INSERT into collection_log while the transaction was doomed (XACT_STATE = -1), swallowing the real error with "The current transaction cannot be committed." Same pattern in ensure_collection_table where INSERT happened before ROLLBACK. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cf355e8 commit 91f46f7

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

install/05_delta_framework.sql

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,38 +1202,43 @@ BEGIN
12021202

12031203
END TRY
12041204
BEGIN CATCH
1205+
DECLARE
1206+
@error_message nvarchar(4000) = ERROR_MESSAGE();
1207+
12051208
/*
12061209
Only rollback if we started the transaction
12071210
Otherwise let the caller handle it
12081211
*/
1209-
IF @trancount_at_entry = 0
1212+
IF @trancount_at_entry = 0
12101213
AND @@TRANCOUNT > 0
12111214
BEGIN
12121215
ROLLBACK TRANSACTION;
12131216
END;
12141217

1215-
DECLARE
1216-
@error_message nvarchar(4000) = ERROR_MESSAGE();
1217-
12181218
/*
1219-
Log the error
1219+
Log the error only if the transaction is not doomed
1220+
When called inside a caller's transaction that is doomed (XACT_STATE = -1),
1221+
we cannot write to the log — the caller must rollback first
12201222
*/
1221-
INSERT INTO
1222-
config.collection_log
1223-
(
1224-
collector_name,
1225-
collection_status,
1226-
duration_ms,
1227-
error_message
1228-
)
1229-
VALUES
1230-
(
1231-
N'calculate_deltas_' + @table_name,
1232-
N'ERROR',
1233-
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
1234-
@error_message
1235-
);
1236-
1223+
IF XACT_STATE() <> -1
1224+
BEGIN
1225+
INSERT INTO
1226+
config.collection_log
1227+
(
1228+
collector_name,
1229+
collection_status,
1230+
duration_ms,
1231+
error_message
1232+
)
1233+
VALUES
1234+
(
1235+
N'calculate_deltas_' + @table_name,
1236+
N'ERROR',
1237+
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
1238+
@error_message
1239+
);
1240+
END;
1241+
12371242
RAISERROR(N'Error calculating deltas for %s: %s', 16, 1, @table_name, @error_message);
12381243
END CATCH;
12391244
END;

install/06_ensure_collection_table.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,14 @@ BEGIN
12061206
BEGIN CATCH
12071207
SET @error_message = ERROR_MESSAGE();
12081208

1209+
IF @@TRANCOUNT > 0
1210+
BEGIN
1211+
ROLLBACK;
1212+
END;
1213+
12091214
/*
12101215
Log errors to collection log
1216+
Must happen after rollback to avoid doomed transaction writes
12111217
*/
12121218
INSERT INTO
12131219
config.collection_log
@@ -1229,11 +1235,6 @@ BEGIN
12291235
@error_message
12301236
);
12311237

1232-
IF @@TRANCOUNT > 0
1233-
BEGIN
1234-
ROLLBACK;
1235-
END;
1236-
12371238
THROW;
12381239
END CATCH;
12391240
END;

0 commit comments

Comments
 (0)