Skip to content

fix: resolve issue with S3 objects not deleting on /full route#269

Merged
asternic merged 7 commits intoasternic:mainfrom
devLucasMoraes:fix-s3-deletion
Mar 2, 2026
Merged

fix: resolve issue with S3 objects not deleting on /full route#269
asternic merged 7 commits intoasternic:mainfrom
devLucasMoraes:fix-s3-deletion

Conversation

@devLucasMoraes
Copy link
Contributor

This PR fixes the bug described in #268 .
It reorders the logic in handlers.go -> DeleteUserComplete() so that the database is queried for the user's s3_enabled status before their record is deleted from the users table. This prevents the S3 deletion step from being silently skipped due to an empty database result.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug preventing S3 objects from being properly deleted when a user's account was fully removed. It achieves this by resequencing database operations to ensure S3 configuration is retrieved before user data is purged. Additionally, it introduces a more resilient lazy initialization strategy for S3 clients, ensuring that S3 operations can proceed even if clients weren't explicitly set up during initial startup or after service restarts, thereby enhancing the overall stability and correctness of S3 integrations.

Highlights

  • S3 Object Deletion Fix: Reordered the logic in the DeleteUserComplete handler to query a user's S3 configuration (s3_enabled) from the database before deleting their user record. This resolves a bug where S3 objects were not being deleted because the s3_enabled status could not be retrieved after the user was removed from the database.
  • Lazy S3 Client Initialization: Implemented a new mechanism for lazy S3 client initialization. The S3Manager can now retrieve S3 configuration directly from the database and initialize a client on demand, improving robustness for scenarios like user reconnects after service restarts or when S3 clients are not pre-initialized.
  • S3 Configuration in User Context: Updated the user authentication process to include s3_enabled and media_delivery status in the cached user information, making these details readily available for S3-related operations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • handlers.go
    • Modified the authalice function's SQL query to fetch s3_enabled and media_delivery fields, storing them in the user's cached Values.
    • Reordered steps in DeleteUserComplete to query s3_enabled status before deleting the user from the database, ensuring S3 deletion logic can execute correctly.
    • Updated step numbering in DeleteUserComplete comments to reflect the reordered logic.
  • helpers.go
    • Added a call to ensureS3ClientForUser before processing S3 uploads in ProcessOutgoingMedia to ensure the S3 client is initialized.
  • main.go
    • Set the database reference in the S3Manager during application startup to enable lazy S3 client initialization.
  • s3manager.go
    • Imported sqlx for database interactions.
    • Added a db field to the S3Manager struct to hold the database connection.
    • Implemented SetDB method to assign the database connection to the S3Manager.
    • Introduced EnsureClientFromDB method to lazily load S3 configuration from the database and initialize an S3 client if one doesn't already exist for a given user.
    • Modified UploadToS3 to attempt lazy client initialization via EnsureClientFromDB if an S3 client is not found, improving resilience.
  • wmiau.go
    • Added a new helper function ensureS3ClientForUser to encapsulate the call to S3Manager.EnsureClientFromDB.
    • Replaced explicit S3 configuration loading and client initialization logic in connectOnStartup with a call to ensureS3ClientForUser.
    • Added a call to ensureS3ClientForUser in startClient to handle S3 client initialization for users reconnecting after service restarts.
    • Integrated ensureS3ClientForUser calls at multiple points within myEventHandler before S3 media processing to guarantee client availability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes a bug where S3 objects were not being deleted upon user removal by reordering the database queries. The introduction of a lazy-initialization mechanism for the S3 client is also a good structural improvement. However, the changes introduce a new critical bug in handlers.go that could prevent S3 object deletion under certain error conditions. Additionally, there is some code redundancy in wmiau.go that could be refactored. My review includes comments on these points.

// 5. Remove files from S3 (if enabled)
var s3Enabled bool
err = s.db.QueryRow("SELECT s3_enabled FROM users WHERE id = $1", id).Scan(&s3Enabled)
// 6. Remove files from S3 (if enabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There's a subtle bug in the if condition on the next line (if err == nil && s3Enabled). The err variable it checks is from the os.RemoveAll operation on line 5296. If local file deletion fails, this will prevent S3 objects from being deleted.

To fix this, the condition on line 5303 should be changed to just if s3Enabled. The s3Enabled flag will correctly be false if the initial database query for it failed, so checking err here is both incorrect and unnecessary.


// Process S3 upload if enabled
if s3Config.Enabled == "true" && (s3Config.MediaDelivery == "s3" || s3Config.MediaDelivery == "both") {
ensureS3ClientForUser(txtid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This call to ensureS3ClientForUser(txtid) is redundant because an identical check and call is already performed on line 787 for all message events. This redundant call is repeated for all media types (audio, document, video, sticker).

Please remove this line and the similar ones for other media types to improve clarity and avoid unnecessary function calls.

@asternic asternic merged commit 2245e71 into asternic:main Mar 2, 2026
1 check passed
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.

3 participants