Skip to content

fix: duplicate background tasks - #491

Merged
Akinator31 merged 12 commits into
mainfrom
fix/duplicate-background-tasks
Oct 14, 2026
Merged

Akinator31 merged 12 commits into
mainfrom
fix/duplicate-background-tasks

Conversation

@Akinator31

Copy link
Copy Markdown
Member

This pull request introduces several improvements and fixes across the codebase, focusing on database reliability, concurrency, error handling, and code modernization. The most significant changes include making thread closure and message number allocation atomic and robust, improving error logging, updating user fetching methods for consistency, and adding comprehensive tests for database operations.

Database reliability and concurrency improvements:

  • Refactored close_thread to be idempotent, returning a boolean to indicate if the thread was actually closed, and ensuring concurrent close attempts do not overwrite each other's metadata. The SQL update now only affects open threads (status = 1). [1] [2]
  • Rewrote allocate_next_message_number to use a single atomic SQL statement with RETURNING, ensuring unique message numbers even under heavy concurrency, and added a test to verify uniqueness. [1] [2]
  • Reduced SQLite connection pool size and set a shorter acquire timeout to reduce lock contention and fail fast under load.

Error handling and logging:

  • Replaced silent ignores of database update errors (e.g., in update_thread_status_db) with explicit error logging using eprintln! in multiple commands, improving observability of failures. [1] [2] [3] [4] [5] [6] [7]

User fetching modernization:

  • Updated all usages of ctx.http.get_user to use the more idiomatic UserId::to_user(&ctx) method, ensuring consistency and leveraging the latest library patterns. [1] [2] [3] [4] [5]

Testing and code quality:

  • Added comprehensive tests for thread closing and message number allocation, including concurrency checks, to the update_thread_status_db module, improving code safety and reliability.

Other codebase improvements:

  • Introduced a global keyed task manager for reminder timers using LazyLock, ensuring one timer per reminder ID and improving reminder scheduling. [1] [2] [3]
  • Added utility types (FlagGuard) for managing atomic flags in the ready handler, ensuring proper cleanup even if tasks panic.

These changes collectively improve the robustness, maintainability, and observability of the application.

@Akinator31 Akinator31 self-assigned this Oct 14, 2026
@Akinator31 Akinator31 linked an issue Oct 14, 2026 that may be closed by this pull request
@Akinator31
Akinator31 merged commit 52e5f12 into main Oct 14, 2026
2 checks passed
@Akinator31
Akinator31 deleted the fix/duplicate-background-tasks branch October 14, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix sqlite errors queue

1 participant