Mailcow UI "Sync Jobs" Error
Contribution guidelines
- I've read the contribution guidelines and wholeheartedly agree
Checklist prior issue creation
- I understand that failure to follow below instructions may cause this issue to be closed.
- I understand that vague, incomplete or inaccurate information may cause this issue to be closed.
- I understand that this form is intended solely for reporting software bugs and not for support-related inquiries.
- I understand that all responses are voluntary and community-driven, and do not constitute commercial support.
- I confirm that I have reviewed previous issues to ensure this matter has not already been addressed.
- I confirm that my environment meets all prerequisite requirements as specified in the official documentation.
Description
The built-in imapsync sync job (Mailcow UI "Sync Jobs") gets permanently stuck in "pending"/running state after a previous imapsync_runner.pl process is abnormally terminated (e.g. container restart, OOM-kill, SIGKILL) before it reaches its finally block.
Root cause: imapsync_runner.pl uses LockFile::Simple to create a lock file at /tmp/imapsync_busy.lock containing the PID of the running process. LockFile::Simple only checks for the existence of this lock file — it never validates whether the PID stored inside it still refers to a live process. If the perl process holding the lock is killed abruptly (before it calls $lockmgr->unlock($lock_file) in the normal flow at the end of the script), the lock file is never cleaned up and remains on disk forever.
From that point on, every subsequent run of imapsync_runner.pl (triggered every minute by the dovecot_imapsync_runner ofelia job) fails immediately with:
can't lock /tmp/imapsync_busy at /usr/local/bin/imapsync_runner.pl line 42.This happens before the try/catch block wrapping the actual IPC::Run::run(...) imapsync execution, so the database row for the sync job is never updated with a fresh, meaningful error. The row is left with stale values from whatever run originally created the orphaned lock:
is_running = 1
last_run = NULL
success = NULL
exit_status = NULL
returned_text = 'Could not start or finish imapsync'In the Mailcow UI this displays as the sync job permanently stuck on "pending", never updating, even though ofelia is executing the cron job every minute as expected.
Manually running imapsync with the exact same parameters (bypassing the runner script) works correctly and completes the sync without any problem — confirming that the imapsync binary itself, network connectivity, and credentials are all fine. The only broken component is the runner's locking logic.
Steps to reproduce:
- Create/enable an imapsync sync job (Configuration -> Mailboxes -> host -> Sync Jobs) in the Mailcow UI, pointing to any remote IMAP server.
- While
imapsync_runner.plis actively running (inside thedovecot-mailcowcontainer, holding/tmp/imapsync_busy.lock), forcibly kill the process before it completes, simu>(In production this can also happen naturally via container restart, host reboot, or OOM-killer intervention while a large mailbox sync is in progress.)docker exec dovecot-mailcow sh -c 'kill -9 $(pgrep -f imapsync_runner.pl)' - Confirm the stale lock file remains:
docker exec dovecot-mailcow ls -la /tmp/imapsync_busy.lock docker exec dovecot-mailcow cat /tmp/imapsync_busy.lock # PID no longer running - Wait for the next ofelia cron tick (runs every minute) and observe that
imapsync_runner.plimmediately fails withcan't lock ...instead of attempting a new sync — this re> - Check the sync job status in the Mailcow UI — it remains stuck showing "pending" and never updates
last_run/success/exit_status.
Logs:
Ofelia container logs (`docker logs mailcowdockerized-ofelia-mailcow-1 --tail 50 | grep -i imapsync`), repeating every minute indefinitely:
2026-07-27T16:59:02.73+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (ce55e2082dc8)] StdErr: can't lock /tmp/imapsync_busy at /usr/local/bin/imapsync_runner.pl lin>
2026-07-27T16:59:02.73+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (ce55e2082dc8)] Finished in "2.558920186s", failed: false, skipped: false, error: none
2026-07-27T17:00:00.209+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (5d77e56d7c09)] Started - /bin/bash -c "[[ ${MASTER} == y ]] && /usr/local/bin/gosu nobody /u>
2026-07-27T17:00:02.861+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (5d77e56d7c09)] StdErr: can't lock /tmp/imapsync_busy at /usr/local/bin/imapsync_runner.pl li>
2026-07-27T17:00:02.861+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (5d77e56d7c09)] Finished in "2.651947992s", failed: false, skipped: false, error: none
2026-07-27T17:01:00.13+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (00e1cde2799f)] Started - /bin/bash -c "[[ ${MASTER} == y ]] && /usr/local/bin/gosu nobody /us>
2026-07-27T17:01:02.804+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (00e1cde2799f)] StdErr: can't lock /tmp/imapsync_busy at /usr/local/bin/imapsync_runner.pl li>
2026-07-27T17:01:02.804+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (00e1cde2799f)] Finished in "2.674226511s", failed: false, skipped: false, error: none
2026-07-27T17:02:00.195+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (cdef1259cd20)] Started - /bin/bash -c "[[ ${MASTER} == y ]] && /usr/local/bin/gosu nobody /u>
2026-07-27T17:02:02.733+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (cdef1259cd20)] StdErr: can't lock /tmp/imapsync_busy at /usr/local/bin/imapsync_runner.pl li>
2026-07-27T17:02:02.733+03:00 common.go:125 ▶ NOTICE [Job "dovecot_imapsync_runner" (cdef1259cd20)] Finished in "2.538016098s", failed: false, skipped: false, error: none
MySQL `imapsync` table row for the affected sync job, showing the stuck state:
id: 1
user1: tech@*.ru
is_running: 1
last_run: NULL
success: NULL
exit_status: NULL
returned_text: Could not start or finish imapsync
Stale lock file evidence found on disk:
$ docker exec dovecot-mailcow ls -la --time-style=full-iso /tmp/imapsync_busy.lock
-r--r--r-- 1 nobody nobody 5 2026-07-27 16:50:00.618008413 +0300 /tmp/imapsync_busy.lock
$ docker exec dovecot-mailcow cat /tmp/imapsync_busy.lock
8264
$ docker exec dovecot-mailcow ps -p 8264
PID TTY TIME CMD
(no such process)Which branch are you using?
master (stable)
Which architecture are you using?
x86_64
Operating System:
Ubuntu 24.04.4 LTS
Server/VM specifications:
6 vCPU, 11 GiB RAM (KVM VM)
Is Apparmor, SELinux or similar active?
No (neither sestatus nor aa-status present/active on host)
Virtualization technology:
KVM
Docker version:
29.6.1
docker-compose version or docker compose version:
v5.3.1
mailcow version:
2026-07 (latest tag on master at time of report)
Reverse proxy:
Custom setup — external Nginx Proxy Manager (jc21/nginx-proxy-manager:latest) in front of mailcow's own ghcr.io/mailcow/nginx:1.30.3-1, forwarding to mailcow's internal nginx on the docker network. Not the stock mailcow reverse proxy setup, but this should not be related to the imapsync issue since it's purely a backend/perl-script logic bug unrelated to HTTP routing.
Logs of git diff:
Only the fix described below was applied locally (see diff section). No other changes to the codebase.
--- a/data/Dockerfiles/dovecot/imapsync_runner.pl
+++ b/data/Dockerfiles/dovecot/imapsync_runner.pl
@@ -38,6 +38,35 @@ sub qqw($) {
$run_dir="/tmp";
$dsn = 'DBI:mysql:database=' . $ENV{'DBNAME'} . ';mysql_socket=/var/run/mysqld/mysqld.sock';
$lock_file = $run_dir . "/imapsync_busy";
+
+# Stale lock detection: LockFile::Simple only checks for file existence,
+# it does not verify whether the PID stored in the lock is still alive.
+# If a previous run was killed (OOM, container restart, SIGKILL) before
+# reaching the unlock() call, the lock file survives forever and blocks
+# every future run with "can't lock ...". Detect and clear such stale
+# locks before attempting to acquire the lock ourselves.
+my $lock_data_file = "${lock_file}.lock";
+if (-e $lock_data_file) {
+ open(my $lfh, '<', $lock_data_file) or undef $lfh;
+ my $stale_pid;
+ if ($lfh) {
+ $stale_pid = <$lfh>;
+ close $lfh;
+ $stale_pid = trim($stale_pid) if defined $stale_pid;
+ }
+ if (defined $stale_pid && $stale_pid =~ /^\d+$/) {
+ if (kill(0, $stale_pid) == 0) {
+ # Process is gone but lock file remains -> stale lock, remove it.
+ print "Removing stale imapsync lock (dead pid ${stale_pid})\n";
+ unlink $lock_data_file;
+ }
+ } else {
+ # Lock file exists but has no readable/valid PID content -> also stale.
+ print "Removing unreadable/invalid imapsync lock file\n";
+ unlink $lock_data_file;
+ }
+}
+
$lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
$lockmgr->lock($lock_file) || die "can't lock ${lock_file}";
$dbh = DBI->connect($dsn, $ENV{'DBUSER'}, $ENV{'DBPASS'}, {
### Line numbers (context for reviewers)
Insertion point: between the original lines 40 and 41 of `data/Dockerfiles/dovecot/imapsync_runner.pl` (right after `$lock_file = $run_dir . "/imapsync_busy";` and before `$lockmgr = LockFile::Simple->make(...)`). New code adds 29 lines (lines 41–69 in the patched file); the original `$lockmgr->lock(...)` line, previously line 41, is now line 70.
### Verification performed
- `perl -c imapsync_runner.pl` inside the `dovecot-mailcow` container: syntax OK.
- Isolated unit test of the stale-detection snippet alone, confirming: a dead PID (e.g. `999999`) triggers removal of the lock file; a live PID leaves the lock untouched.
- Applied the fix live (hot-copied into the running container without restarting it, to avoid interrupting the on-going manual sync), and confirmed on the next ofelia tick that a previously-stuck sync job (which had been failing every minute with `can't lock ...`) proceeded to actually spawn `imapsync` and establish a live TCP connection to the remote IMAP host, transferring mail successfully.
### Workaround (until fixed upstream)
Manually remove the stale lock file when this happens:
docker exec dovecot-mailcow rm -f /tmp/imapsync_busy.lock /tmp/imapsync.*
The sync job will resume on the next ofelia cron tick (within one minute).Logs of iptables -L -vn:
Not available/applicable — the host's Docker installation uses `iptables-nft`/nftables backend and the classic `iptables` binary was not present on the host to query directly (`iptables: command not found`). This is unrelated to the bug being reported (a pure Perl script logic issue inside the `dovecot-mailcow` container), so it was not pursued further. Happy to gather nftables-equivalent output on request.Logs of ip6tables -L -vn:
Not available/applicable — the host's Docker installation uses `iptables-nft`/nftables backend and the classic `iptables` binary was not present on the host to query directly (`iptables: command not found`). This is unrelated to the bug being reported (a pure Perl script logic issue inside the `dovecot-mailcow` container), so it was not pursued further. Happy to gather nftables-equivalent output on request.Logs of iptables -L -vn -t nat:
Not available/applicable — the host's Docker installation uses `iptables-nft`/nftables backend and the classic `iptables` binary was not present on the host to query directly (`iptables: command not found`). This is unrelated to the bug being reported (a pure Perl script logic issue inside the `dovecot-mailcow` container), so it was not pursued further. Happy to gather nftables-equivalent output on request.Logs of ip6tables -L -vn -t nat:
Not available/applicable — the host's Docker installation uses `iptables-nft`/nftables backend and the classic `iptables` binary was not present on the host to query directly (`iptables: command not found`). This is unrelated to the bug being reported (a pure Perl script logic issue inside the `dovecot-mailcow` container), so it was not pursued further. Happy to gather nftables-equivalent output on request.DNS check:
$ docker exec $(docker ps -qf name=acme-mailcow) dig +short stackoverflow.com @172.*.*.254
198.*.*.1
DNS resolution inside the mailcow docker network works correctly.Source: mailcow/mailcow-dockerized