Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enteauth deleting the entire ~/.cache folder on linux #4536

Open
gepbird opened this issue Dec 29, 2024 · 4 comments
Open

enteauth deleting the entire ~/.cache folder on linux #4536

gepbird opened this issue Dec 29, 2024 · 4 comments
Assignees
Labels
- auth Relates to Ente Auth --desktop Plaftorm is desktop

Comments

@gepbird
Copy link

gepbird commented Dec 29, 2024

Description

A few minutes after running enteauth, I noticed other programs misbehaving, failing to read files in ~/.cache, and I was surprised to see my cache directory being pretty much empty (~300KB) when it's usually hundreds of MB. This happened for the 3rd time.

Unfortunately I can't consistently reproduce this by opening and closing enteauth (maybe it does it only after a specific amount of time has passed?).

By peeking at the code, looks like it's deleting tempDirectory that is being set to cacheHome which resolves to $XDG_CACHE_HOME that is ~/.cache.

It should only delete enteauth's data, something like ~/.cache/enteauth.

Looks like this bug was introduced in 2e01a96 (#3334), cc @prateekmedia

Related: #2563

Version

4.1.6

What product are you using?

Ente Auth

What platform are you using?

Desktop - Linux

@gepbird gepbird added the triage label Dec 29, 2024
gepbird added a commit to gepbird/dotfiles that referenced this issue Dec 29, 2024
@prateekmedia
Copy link
Member

Will be fixed in next release.

@stancel
Copy link

stancel commented Dec 30, 2024

Wow, thank you for opening this issue as well as for the upcoming fix! I have been going nuts trying to track down why my ~/.cache folder contents are being periodically wiped out. It is killing the ibus socket files stored there that eventually causes the keyboard input not to work, among many other issues. I have been searching through system logs and putting scripts in place on cron jobs that backup my ~/.cache directory and then restore it has been wiped. The timestamps of this logging I put in place of this happening matches my times using and closing out enteauth. I was also able to just recreate the issue, confirming it is wiping out all contents under ~/.cache/*. Thank you for finding and diagnosing this issue and for the upcoming fix. Will check for an update now. Was on 4.1.6 when adding this comment and testing the issue.

@ua741 ua741 added --desktop Plaftorm is desktop - auth Relates to Ente Auth and removed triage labels Jan 1, 2025
@stancel
Copy link

stancel commented Jan 4, 2025

Unfortunately, it looks like the fix for this issue did not make it into the 4.2.3 release that I updated to earlier today. Just had the contents of my ~/.cache/ folder wiped again when I opened EnteAuth. In case it helps anyone else, here is a script that I have come up with to periodically backup, restore the ~/.cache directory along with restarting IBus so that my keyboard does not quit working when it erases the IBus unix socket file that is for some now placed under the ~/.cache/ directory in Ubuntu 24.04. Hopefully this may help bring someone some sanity and save them some time in triaging the issue in a roundabout way until this issue can be included in an upcoming release.

I created a script called restore-cache.sh with the below contents and then put it on a cron job to run every five minutes. After several revisions, this version seems to be working well. Please feel free to use and/or modify it in any way that suits your needs.

#!/bin/bash

# Set variables
LOGFILE="$HOME/.cache_restore.log"
CACHE_DIR="$HOME/.cache"
BACKUP_DIR="$HOME/.cache_backup"
IBUS_DIR="$CACHE_DIR/ibus"
IBUS_BACKUP_DIR="$BACKUP_DIR/ibus"
IBUS_SOCKET_PATTERN="dbus-*"
EXCLUDED_PATTERN="event-sound-cache.tdb.*.x86_64-pc-linux-gnu"

# Set environment variables for cron
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

log() {
    echo "$(date): $1" >> "$LOGFILE"
}

log_system_state() {
    log "System state:"
    log "IBus processes:"
    ps aux | grep [i]bus >> "$LOGFILE" 2>&1
    log "IBus directory contents:"
    ls -lah "$IBUS_DIR" >> "$LOGFILE" 2>&1
    log "Backup directory contents:"
    ls -lah "$BACKUP_DIR" >> "$LOGFILE" 2>&1
    log "Cache directory contents:"
    ls -lah "$CACHE_DIR" >> "$LOGFILE" 2>&1
}

validate_cache() {
    # Check if critical IBus directory and socket file exist
    if [ -d "$IBUS_DIR" ] && [ -S "$IBUS_DIR/$(find "$IBUS_DIR" -maxdepth 1 -type s -name "$IBUS_SOCKET_PATTERN" 2>/dev/null | head -n 1)" ]; then
        return 0
    fi

    # Check if ~/.cache contains only the excluded pattern
    local non_excluded_files=$(find "$CACHE_DIR" -mindepth 1 \! -name "$EXCLUDED_PATTERN" 2>/dev/null)
    if [ -z "$non_excluded_files" ]; then
        return 1
    fi

    return 1
}

validate_backup() {
    # Check if backup contains valid IBus directory and socket file
    if [ -d "$IBUS_BACKUP_DIR" ] && [ -S "$IBUS_BACKUP_DIR/$(find "$IBUS_BACKUP_DIR" -maxdepth 1 -type s -name "$IBUS_SOCKET_PATTERN" 2>/dev/null | head -n 1)" ]; then
        return 0
    else
        return 1
    fi
}

restore_cache_from_backup() {
    log "Restoring ~/.cache from backup."
    rm -rf "$CACHE_DIR"
    cp -a "$BACKUP_DIR/." "$CACHE_DIR"
    log "Cache restoration complete."
}

backup_cache() {
    log "Synchronizing $CACHE_DIR to $BACKUP_DIR."
    rm -rf "$BACKUP_DIR"
    cp -a "$CACHE_DIR/." "$BACKUP_DIR"
    log "Backup synchronization complete."
}

setup_ibus() {
    log "Restarting IBus and ensuring proper setup."
    rm -rf "$IBUS_DIR"
    ibus restart
    sleep 15

    # Ensure the IBus directory exists after restart
    mkdir -p "$IBUS_DIR/bus"

    # Locate the socket file
    SOCKET_FILE=$(find "$IBUS_DIR" -maxdepth 1 -type s -name "$IBUS_SOCKET_PATTERN" 2>/dev/null | head -n 1)

    if [ -n "$SOCKET_FILE" ]; then
        # Remove any existing incorrect symbolic links
        find "$IBUS_DIR/bus" -type l -delete

        # Create the symbolic link to the socket file
        ln -sf "$SOCKET_FILE" "$IBUS_DIR/bus/"
        log "IBus setup restored successfully."
    else
        log "IBus socket file missing after restart. Manual intervention required."
    fi
}

# Main logic
log "Starting cache restore script."

log_system_state  # Line 93: Log the initial system state

# Validate ~/.cache
if ! validate_cache; then
    log "~/.cache is missing or invalid. Restoring from backup."
    if validate_backup; then
        restore_cache_from_backup
    else
        log "Backup is also invalid. Attempting to recreate critical directories."
        mkdir -p "$IBUS_DIR"
    fi
    setup_ibus
else
    log "~/.cache is valid."
fi

# Backup cache if valid
if validate_cache; then
    backup_cache
else
    log "Cache is invalid. Cannot update backup."
fi

log_system_state  # Line 128: Log the final system state

log "Cache restore check complete."

Hope this helps in the meantime or that the fix is included for this issue in the next release.

@gepbird
Copy link
Author

gepbird commented Jan 5, 2025

@prateekmedia any updates on this? It's causing issues for more and more people and I'd say it's a pretty high priority issue.

For people who use nix or can compile from source, this patch could be useful as a workaround:

  ente-auth = pkgs.ente-auth.overrideAttrs (o: {
    prePatch = ''
      substituteInPlace lib/utils/directory_utils.dart \
        --replace-fail "return cacheHome" "return Directory(p.join(cacheHome.path, 'enteauthinit'));"
    '';
  });

I'll look into submitting a proper PR tomorrow if there still won't be any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- auth Relates to Ente Auth --desktop Plaftorm is desktop
Projects
None yet
Development

No branches or pull requests

4 participants