Bug report: whole panel freezes (deadlock) when applying SSL / adding a site — sqlite_easy file lock + gevent + uncommitted transaction in the disk-category inotify writer
Environment
- aaPanel 8.0.7 (Linux, EN build;
updateLinuxEn API confirms 8.0.7 is the latest stable at the time of writing)
- Ubuntu 24.04.5 LTS, kernel 6.8.0-147-generic, fresh install on 2026-10-28
- Panel Python:
/www/server/panel/pyenv/bin/python3 3.12.3
- Web server: nginx; MariaDB; 6 vCPU / 12 GB RAM, no swap pressure, disk 3% used
- Outbound network and DNS are fine (acme-v02.api.letsencrypt.org, www.aapanel.com, api.aapanel.com all reachable in < 1.2 s)
Symptom
- Create a website, then click Apply SSL (Let's Encrypt, HTTP-01 file verification), or let "Site Initialize" auto-apply SSL.
- Within 20 seconds the entire panel stops responding. Every request hangs, the UI shows a loading spinner forever. The nginx
webserver on port 22159 still accepts connections, but BT-Panel never reads from /tmp/panel.sock (Recv-Q grows).
- Only
bt restart or a server reboot recovers it. Reproduced 3 times in 40 minutes on a fresh install.
- After the restart the SSL task stays in
dns_domain_task with task_status = 30 ("running") forever and is never resumed; the certificate is never issued.
What I found (evidence)
1. BT-Panel is blocked in a kernel flock() call, waiting for a lock that it holds itself
/proc/locks while the panel was frozen (inode 787399 = /tmp/aap_locks/____www____server____panel____data____default.db.lock, PID 1389 = BT-Panel, 1293 = BT-Task, 1742/1754 = BTTask/task_script.py subprocesses):
2: FLOCK ADVISORY WRITE 1389 08:01:787399 0 EOF <- BT-Panel HOLDS LOCK_EX (fd 6, opened "rb+" by __wlock)
2: -> FLOCK ADVISORY WRITE 1293 08:01:787399 0 EOF <- BT-Task waiting for LOCK_EX
2: -> FLOCK ADVISORY READ 1389 08:01:787399 0 EOF <- BT-Panel ITSELF waiting for LOCK_SH (fd 12, opened "rb" by __rlock)
2: -> FLOCK ADVISORY READ 1742 08:01:787399 0 EOF <- task_script update_monitor_requests waiting
2: -> FLOCK ADVISORY READ 1754 08:01:787399 0 EOF <- task_script make_suer_ssl_task waiting
strace -p 1389 shows the one and only thread of BT-Panel stuck in flock(12, LOCK_SH. /proc/1389/wchan = locks_lock_inode_wait.
py-spy dump --pid 1389 (BT-Panel):
Thread 1389 (idle)
__rlock (public/sqlite_easy.py:940)
__enter__ (contextlib.py:137)
query (public/sqlite_easy.py:1056)
query (public/sqlite_easy.py:2203)
select (public/sqlite_easy.py:2564)
__execute (public/aaModel/manager.py:580)
as_list (public/aaModel/manager.py:764)
ssl_tasks_status (ssl_domainModelV2/api.py:1400) <- the frontend's 3-second polling request
run (BTPanel/__init__.py:3461)
...
handle_one_response (gevent/pywsgi.py:1107)
2. BT-Task holds an open, uncommitted SQLite write transaction on data/default.db
/proc/locks (inode 23085665 = /www/server/panel/data/default.db):
7: POSIX ADVISORY WRITE 1293 08:01:23085665 1073741825 1073741825 <- SQLite RESERVED lock held by BT-Task
8: POSIX ADVISORY READ 1293 08:01:23085665 1073741826 1073742335 <- SQLite SHARED lock held by BT-Task
default.db-journal exists (4640 bytes) and strings on it shows the tables/paths being written: disk_category_dirty, system_logs_size/www/wwwlogs, system_logs_size/www/server/panel/logs.
py-spy dump --pid 1293 (BT-Task) — the thread that owns that transaction is now waiting for the file lock again:
Thread 1541 (idle): "disk-category-inotify"
__wlock (public/sqlite_easy.py:950)
__enter__ (contextlib.py:137)
execute (public/sqlite_easy.py:1126)
execute (public/sqlite_easy.py:2237)
_update_disk_category_dirty_file (system_v2.py:996)
_disk_category_inotify_loop (system_v2.py:1369)
3. Logs
logs/sqlite_easy/202610/28.log — database is locked from the panel's retry loop, every 15 s (the 15 s sqlite3.connect(timeout=15) busy timeout) for 6+ minutes (= 25 retries of __sqlite_retry_help), then the same right before each freeze:
[2026-10-28 14:30:32]UPDATE `dns_domain_ssl` SET `log` = ?, `update_time` = ? WHERE `id` = ?
[2026-10-28 14:30:32]database is locked
... (repeated every 15 s until 14:36:47)
[2026-10-28 14:52:42]UPDATE `dns_domain_task` SET `task_status` = ? WHERE `id` = ? -> database is locked (freeze #1)
[2026-10-28 15:02:29]UPDATE `dns_domain_task` SET `task_status` = ? WHERE `id` = ? -> database is locked (freeze #2)
[2026-10-28 15:07:00]UPDATE `dns_domain_task` SET `task_status` = ? WHERE `id` = ? -> database is locked (freeze #3)
logs/error.log: [2026-10-28 14:36:47][DEBUG] - update ssl link error: database is locked
logs/request/2026-10-28.json: the last request ever logged before freeze #3 is POST /v2/ssl_domain?action=apply_new_ssl at 15:06:43 (answered 200 in 442 ms), followed by the ssl_tasks_status polls; nothing is logged after that.
Root cause
Three things in class/public/sqlite_easy.py and class_v2/system_v2.py interact:
(a) The file lock and the SQLite lock are acquired in opposite order (lock-order inversion)
sqlite_easy.Sqlite.execute() (line 1126) does:
with self.__wlock(): # open lock file, fcntl.flock(LOCK_EX)
return self.__sqlite_retry_help(self.__execute_help, sql, params, get_rowid)
# LOCK_EX released here, when the file is closed
__execute_help never commits. The connection is created with isolation_level = 'IMMEDIATE', so the first DML statement issues BEGIN IMMEDIATE and the SQLite RESERVED lock stays held until commit(), which only happens later in the wrapper (line ~2245: if self.__is_autocommit(): self.commit()) — i.e. after the file lock has already been released.
class_v2/system_v2.py turns autocommit off and runs many statements before committing, in four places:
_save_disk_category_project_state (line 778)
_update_disk_category_dirty_file (lines 954 and 972 — this one is called from the disk-category-inotify thread on every inotify event batch)
_finish_disk_category_dirty_snapshot (line 1100)
with public.SqliteConn('default') as db:
db.autocommit(False)
for event in event_values:
row = db.query().query('SELECT ... FROM `disk_category_dirty` ...') # LOCK_SH, released
db.query().execute('INSERT OR IGNORE INTO `disk_category_dirty` ...') # LOCK_EX, released; RESERVED lock now held
db.query().execute('UPDATE `disk_category_dirty` ...') # LOCK_EX again
db.commit()
So between statements BT-Task holds the SQLite RESERVED lock but not the file lock. Any other writer (BT-Panel) can now take the file lock, then blocks inside SQLite for the full 15 s busy timeout and fails with database is locked. BT-Task, in turn, needs the file lock for its next statement — which BT-Panel holds — and can never reach commit(). That is already a cross-process deadlock.
(b) In BT-Panel the retry loop yields to gevent while holding LOCK_EX, and flock() is not gevent-aware
BT-Panel runs gevent.monkey.patch_all(). __sqlite_retry_help handles database is locked with time.sleep(retry_interval * 0.001) — under gevent this switches to another greenlet while the current greenlet still holds LOCK_EX. The next greenlet is typically the frontend's ssl_tasks_status poll (every 3 s). It calls query() → __rlock() → fcntl.flock(fp.fileno(), LOCK_SH) on a new file descriptor. flock locks are per open-file-description, so the LOCK_SH conflicts with the LOCK_EX held on the other fd of the same process, and fcntl.flock is a blocking syscall that gevent cannot interrupt. The single OS thread of BT-Panel is now parked in the kernel; the greenlet that owns LOCK_EX can never run again to release it. The panel is dead until killed.
(c) Nobody can recover
BT-Task waits for LOCK_EX, every task_script.py subprocess waits for LOCK_SH, BT-Panel is stuck in the kernel. Hot default.db-journal is left behind. After a restart, the apply_cert work (which runs in a threading.Thread inside BT-Panel) is gone, but the dns_domain_task row stays at task_status = 30 and make_suer_ssl_task does not pick it up.
Why "apply SSL" triggers it almost every time
The inotify watcher covers /www/wwwroot/*, /www/wwwlogs, /www/server/panel/logs, backup and database directories. Applying SSL writes .well-known/acme-challenge files into the site root, reloads nginx (log writes), updates dns_domain_task / dns_domain_ssl.log every 15 s, and the UI polls ssl_tasks_status every 3 s. Both writers are therefore active at the same moment, and the panel has a greenlet ready to call flock(LOCK_SH). But the bug is not SSL-specific: on a site with real traffic the access logs keep inotify busy permanently, so any panel write (add site, cron, FTP, database, firewall rule, settings) can deadlock the same way.
Steps to reproduce
- Fresh aaPanel 8.0.7 install with nginx.
- Create a website whose domain resolves to the server.
- Website → SSL → Let's Encrypt → file verification → Apply (or just rely on the "Site Initialize" auto-SSL task).
- Keep the SSL dialog open so the UI polls
ssl_tasks_status.
- Within 20–60 s the panel stops responding.
cat /proc/locks | grep <inode of /tmp/aap_locks/...default.db.lock> shows BT-Panel holding WRITE and waiting READ on the same inode; default.db-journal is present and strings on it shows disk_category_dirty.
Having some file activity in /www/wwwlogs (e.g. ab/curl loop against the site) makes it reproduce faster.
Workaround I applied (confirmed working)
In /www/server/panel/class_v2/system_v2.py changed the four db.autocommit(False) calls (lines 778, 954, 972, 1100) to db.autocommit(True) so every statement commits immediately and the SQLite RESERVED lock is never held across a file-lock release. After bt restart: zero database is locked entries, no flock waiters in /proc/locks for over an hour, and the SSL application that used to freeze the panel completed in 9 s. autocommit(False) is not used anywhere else in the panel code (the only other BEGIN in aaModel/manager.py is the one-off table-migration path; safecloudModel.py uses its own dir_record.db).
Suggested fixes
- Hold the file lock for the whole transaction. When autocommit is off, acquire
LOCK_EX at the first statement and release it only on commit() / rollback() / close() — or at least never release __wlock while self.__CONN.in_transaction is true. Alternatively make the disk-category writers use a single execute_script / explicit lock around the whole batch.
- Never block in
fcntl.flock() from the gevent-patched BT-Panel. Use LOCK_NB and retry with gevent.sleep, or combine an in-process gevent lock (so two greenlets can never contend on the kernel flock) with the cross-process flock. At minimum, don't time.sleep() (gevent switch) inside __sqlite_retry_help while LOCK_EX is held — release the file lock before sleeping and re-acquire it after.
- Consider
PRAGMA journal_mode=WAL for default.db to reduce reader/writer contention (currently delete).
- After a panel restart, mark or resume
dns_domain_task rows left at task_status = 30; otherwise they show as "running" forever in the UI.
I can provide the full /proc/locks, py-spy dumps, sqlite_easy log, request log and strace output on request.