Skip to content

Commit 6d98462

Browse files
jpnurmiclaude
andcommitted
Make flush delay-aware so it waits for delayed tasks
The flush marker now uses the last task's deadline (capped at the flush timeout) so it sorts after all current tasks including delayed ones. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5f48f12 commit 6d98462

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/sentry_sync.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,23 @@ sentry__bgworker_flush(sentry_bgworker_t *bgw, uint64_t timeout)
361361
sentry__cond_init(&flush_task->signal);
362362
sentry__mutex_init(&flush_task->lock);
363363

364+
// flush potential delayed tasks up until the timeout
365+
uint64_t delay_ms = 0;
366+
uint64_t before = sentry__monotonic_time();
367+
sentry__mutex_lock(&bgw->task_lock);
368+
if (bgw->last_task && bgw->last_task->execute_after > before) {
369+
delay_ms = bgw->last_task->execute_after - before;
370+
if (delay_ms > timeout) {
371+
delay_ms = timeout;
372+
}
373+
}
374+
sentry__mutex_unlock(&bgw->task_lock);
375+
364376
sentry__mutex_lock(&flush_task->lock);
365377

366378
/* submit the task that triggers our condvar once it runs */
367-
sentry__bgworker_submit(bgw, sentry__flush_task,
368-
(void (*)(void *))sentry__flush_task_decref, flush_task);
379+
sentry__bgworker_submit_delayed(bgw, sentry__flush_task,
380+
(void (*)(void *))sentry__flush_task_decref, flush_task, delay_ms);
369381

370382
uint64_t started = sentry__monotonic_time();
371383
bool was_flushed = false;

tests/unit/test_sync.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,14 @@ SENTRY_TEST(bgworker_task_delay)
215215
bgw, record_order_task, NULL, (void *)1, 50);
216216

217217
sentry__bgworker_start(bgw);
218-
TEST_CHECK_INT_EQUAL(sentry__bgworker_shutdown(bgw, 500), 0);
218+
TEST_CHECK_INT_EQUAL(sentry__bgworker_flush(bgw, 500), 0);
219219
uint64_t after = sentry__monotonic_time();
220220

221221
TEST_CHECK_INT_EQUAL(os.count, 1);
222222
TEST_CHECK_INT_EQUAL(os.order[0], 1);
223223
TEST_CHECK(after - before >= 50);
224224

225+
sentry__bgworker_shutdown(bgw, 500);
225226
sentry__bgworker_decref(bgw);
226227
}
227228

@@ -264,7 +265,7 @@ SENTRY_TEST(bgworker_delayed_tasks)
264265
sentry__bgworker_submit(bgw, record_order_task, NULL, (void *)10);
265266

266267
sentry__bgworker_start(bgw);
267-
TEST_CHECK_INT_EQUAL(sentry__bgworker_shutdown(bgw, 5000), 0);
268+
TEST_CHECK_INT_EQUAL(sentry__bgworker_flush(bgw, 5000), 0);
268269

269270
// all tasks execute: immediate first, then delayed in deadline order
270271
TEST_CHECK_INT_EQUAL(os.count, 10);
@@ -279,6 +280,7 @@ SENTRY_TEST(bgworker_delayed_tasks)
279280
TEST_CHECK_INT_EQUAL(os.order[8], 4);
280281
TEST_CHECK_INT_EQUAL(os.order[9], 5);
281282

283+
sentry__bgworker_shutdown(bgw, 500);
282284
sentry__bgworker_decref(bgw);
283285
}
284286

0 commit comments

Comments
 (0)