Skip to content

Navigation Menu

Sign in
Sign up

Prevent deadlock in IMAPServer.close when using 'maxsyncaccounts' and 'maxconnections'. #241

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

Merged
thekix merged 1 commit into OfflineIMAP:master from altruizine:upstream-deadlock-fix
Jun 8, 2026
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions offlineimap/imapserver.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def __init__(self, repos):
self.lastowner = {}
self.semaphore = BoundedSemaphore(self.maxconnections)
self.connectionlock = Lock()
self.closing = False
self.reference = repos.getreference()
self.idlefolders = repos.getidlefolders()
self.gss_vc = None
Expand Down Expand Up @@ -569,6 +570,11 @@ def acquireconnection(self):

self.semaphore.acquire()
self.connectionlock.acquire()
if self.closing:
self.connectionlock.release()
self.semaphore.release()
raise OfflineImapError("Server is closing",
OfflineImapError.ERROR.REPO)
curThread = current_thread()
imapobj = None

Expand Down Expand Up @@ -793,14 +799,17 @@ def connectionwait(self):
self.semaphore.release()

def close(self):
# First make sure no new connections can be established.
self.connectionlock.acquire()
self.closing = True
self.connectionlock.release()

# Make sure I own all the semaphores. Let the threads finish
# their stuff. This is a blocking method.
# Make sure to not call this under connectionlock to avoid deadlocks.
threadutil.semaphorereset(self.semaphore, self.maxconnections)

with self.connectionlock:
# first, wait till all connections had been released.
# TODO: won't work IMHO, as releaseconnection() also
# requires the connectionlock, leading to a potential
# deadlock! Audit & check!
threadutil.semaphorereset(self.semaphore, self.maxconnections)
for imapobj in self.assignedconnections + self.availableconnections:
imapobj.logout()
self.assignedconnections = []
Expand All @@ -809,6 +818,7 @@ def close(self):
# reset GSSAPI state
self.gss_vc = None
self.gssapi = False
self.closing = False

def keepalive(self, timeout, event):
"""Sends a NOOP to each connection recorded.
Expand Down

AltStyle によって変換されたページ (->オリジナル) /