[PR #2333] [MERGED] fix: defer event dispatch until after transaction commit #4067

Closed
opened 2026-03-22 15:00:44 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/go-vikunja/vikunja/pull/2333
Author: @kolaente
Created: 3/2/2026
Status: Merged
Merged: 3/3/2026
Merged by: @kolaente

Base: mainHead: fix-webhook-stale-data


📝 Commits (10+)

  • 18ef58a feat(events): add DispatchOnCommit/DispatchPending for deferred event dispatch
  • 32a67d1 feat(handlers): dispatch pending events after transaction commit
  • 7f30b8a fix(events): defer task event dispatch until after transaction commit
  • dc1430c fix(events): defer event dispatch for task sub-entities
  • 2dc92f2 fix(events): defer event dispatch for project operations
  • 0ff4bb0 fix(events): defer event dispatch for team operations
  • 84ba16b fix(events): defer event dispatch for user creation and task positions
  • ba9ae5e fix(events): dispatch pending events in CalDAV handlers after commit
  • 7eea0bc fix(events): dispatch pending events in migration and export handlers
  • 5db931a test: update event assertions to work with deferred dispatch

📊 Changes

25 files changed (+256 additions, -74 deletions)

View changed files

📝 pkg/events/events.go (+44 -0)
pkg/events/events_test.go (+92 -0)
📝 pkg/models/kanban_task_bucket.go (+2 -1)
📝 pkg/models/project.go (+4 -9)
📝 pkg/models/project_team.go (+1 -4)
📝 pkg/models/project_users.go (+1 -4)
📝 pkg/models/task_assignees.go (+5 -13)
📝 pkg/models/task_attachment.go (+4 -2)
📝 pkg/models/task_comments.go (+6 -3)
📝 pkg/models/task_comments_test.go (+1 -0)
📝 pkg/models/task_position.go (+4 -2)
📝 pkg/models/task_relation.go (+4 -2)
📝 pkg/models/tasks.go (+5 -14)
📝 pkg/models/tasks_test.go (+2 -0)
📝 pkg/models/team_members.go (+4 -7)
📝 pkg/models/teams.go (+4 -2)
📝 pkg/modules/migration/create_from_structure.go (+10 -1)
📝 pkg/routes/api/v1/user_export.go (+4 -5)
📝 pkg/routes/caldav/listStorageProvider.go (+25 -1)
📝 pkg/user/user_create.go (+1 -4)

...and 5 more files

📄 Description

Summary

Fixes #2315

Events (especially webhooks) were being dispatched before database transactions committed, causing webhook listeners that open new database sessions to read stale data.

Changes

New Deferred Event API (pkg/events/events.go)

  • DispatchOnCommit(key, event) - Stores an event to be dispatched later
  • DispatchPending(key) - Dispatches all accumulated events for a key (call after s.Commit())
  • CleanupPending(key) - Discards pending events (call on rollback paths)

Updated CRUD Handlers

All generic CRUD handlers now call events.DispatchPending(s) after commit and events.CleanupPending(s) on rollback paths.

Converted Model Methods

All events.Dispatch() calls in model methods were converted to events.DispatchOnCommit(s, ...):

  • Tasks and task sub-entities (assignees, comments, attachments, relations, kanban buckets)
  • Projects and project sharing (users, teams)
  • Teams and team members
  • User creation
  • Task positions

Updated Non-Handler Call Sites

  • CalDAV handlers
  • Migration (create_from_structure.go)
  • User export

Test Updates

Tests that call model methods directly and assert event dispatch now call events.DispatchPending(s) before assertions.

Testing

  • All existing tests pass
  • Lint passes with 0 issues

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/go-vikunja/vikunja/pull/2333 **Author:** [@kolaente](https://github.com/kolaente) **Created:** 3/2/2026 **Status:** ✅ Merged **Merged:** 3/3/2026 **Merged by:** [@kolaente](https://github.com/kolaente) **Base:** `main` ← **Head:** `fix-webhook-stale-data` --- ### 📝 Commits (10+) - [`18ef58a`](https://github.com/go-vikunja/vikunja/commit/18ef58aca9f2b8f034b9a8942e69d93d3392c94b) feat(events): add DispatchOnCommit/DispatchPending for deferred event dispatch - [`32a67d1`](https://github.com/go-vikunja/vikunja/commit/32a67d19691d3b2d8a1e365d2f42561e0842f55a) feat(handlers): dispatch pending events after transaction commit - [`7f30b8a`](https://github.com/go-vikunja/vikunja/commit/7f30b8a54ce7e2a405e72004025f1c495f4c0e54) fix(events): defer task event dispatch until after transaction commit - [`dc1430c`](https://github.com/go-vikunja/vikunja/commit/dc1430c4c6cc7d54f5d24a13e70e97caa4f727df) fix(events): defer event dispatch for task sub-entities - [`2dc92f2`](https://github.com/go-vikunja/vikunja/commit/2dc92f26424cdab083b22f2116e01486e424b3f1) fix(events): defer event dispatch for project operations - [`0ff4bb0`](https://github.com/go-vikunja/vikunja/commit/0ff4bb02a51d1a545fab5812a9973f238e7e2b24) fix(events): defer event dispatch for team operations - [`84ba16b`](https://github.com/go-vikunja/vikunja/commit/84ba16b801b69d6fdf99f8555905bc5be5b37785) fix(events): defer event dispatch for user creation and task positions - [`ba9ae5e`](https://github.com/go-vikunja/vikunja/commit/ba9ae5eac13db977deb23ef262e289fce0ef7e5f) fix(events): dispatch pending events in CalDAV handlers after commit - [`7eea0bc`](https://github.com/go-vikunja/vikunja/commit/7eea0bc667c8bcd07bd61fa385e103f1a1984402) fix(events): dispatch pending events in migration and export handlers - [`5db931a`](https://github.com/go-vikunja/vikunja/commit/5db931aa846db5444ac921070b4426515defff97) test: update event assertions to work with deferred dispatch ### 📊 Changes **25 files changed** (+256 additions, -74 deletions) <details> <summary>View changed files</summary> 📝 `pkg/events/events.go` (+44 -0) ➕ `pkg/events/events_test.go` (+92 -0) 📝 `pkg/models/kanban_task_bucket.go` (+2 -1) 📝 `pkg/models/project.go` (+4 -9) 📝 `pkg/models/project_team.go` (+1 -4) 📝 `pkg/models/project_users.go` (+1 -4) 📝 `pkg/models/task_assignees.go` (+5 -13) 📝 `pkg/models/task_attachment.go` (+4 -2) 📝 `pkg/models/task_comments.go` (+6 -3) 📝 `pkg/models/task_comments_test.go` (+1 -0) 📝 `pkg/models/task_position.go` (+4 -2) 📝 `pkg/models/task_relation.go` (+4 -2) 📝 `pkg/models/tasks.go` (+5 -14) 📝 `pkg/models/tasks_test.go` (+2 -0) 📝 `pkg/models/team_members.go` (+4 -7) 📝 `pkg/models/teams.go` (+4 -2) 📝 `pkg/modules/migration/create_from_structure.go` (+10 -1) 📝 `pkg/routes/api/v1/user_export.go` (+4 -5) 📝 `pkg/routes/caldav/listStorageProvider.go` (+25 -1) 📝 `pkg/user/user_create.go` (+1 -4) _...and 5 more files_ </details> ### 📄 Description ## Summary Fixes #2315 Events (especially webhooks) were being dispatched before database transactions committed, causing webhook listeners that open new database sessions to read stale data. ## Changes ### New Deferred Event API (pkg/events/events.go) - `DispatchOnCommit(key, event)` - Stores an event to be dispatched later - `DispatchPending(key)` - Dispatches all accumulated events for a key (call after s.Commit()) - `CleanupPending(key)` - Discards pending events (call on rollback paths) ### Updated CRUD Handlers All generic CRUD handlers now call `events.DispatchPending(s)` after commit and `events.CleanupPending(s)` on rollback paths. ### Converted Model Methods All `events.Dispatch()` calls in model methods were converted to `events.DispatchOnCommit(s, ...)`: - Tasks and task sub-entities (assignees, comments, attachments, relations, kanban buckets) - Projects and project sharing (users, teams) - Teams and team members - User creation - Task positions ### Updated Non-Handler Call Sites - CalDAV handlers - Migration (create_from_structure.go) - User export ### Test Updates Tests that call model methods directly and assert event dispatch now call `events.DispatchPending(s)` before assertions. ## Testing - All existing tests pass - Lint passes with 0 issues --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-22 15:00:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/vikunja#4067