Skip to content
Open
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
19 changes: 19 additions & 0 deletions dnf/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import hashlib
import logging
import os
import psutil
import threading
import time

Expand Down Expand Up @@ -114,6 +115,24 @@ def _try_lock(self, pid):
os.write(fd, str(pid).encode('utf-8'))
return pid

# When dnf is killed or exited abnormally, a PID will remain in
# rpmdb_lock.pid, and when the system runs for a period of time
# the PID maybe reused, and at this time, the command will be
# stuck when the dnf operation (such as dnf install) is performed
# again. Therefore, it is not sufficient to detect whether the
# PID exists, and the creation time of rpmdb_lock.pid needs to
# be later than the startup time of pid=PID process is to solve
# this problem.
oldproc = psutil.Process(old_pid)
oldproc_ctime = oldproc.create_time()
lockfile_ctime = os.path.getctime(self.target)
if oldproc_ctime > lockfile_ctime:
# locked by a bad old process, write our pid
os.lseek(fd, 0, os.SEEK_SET)
os.ftruncate(fd, 0)
os.write(fd, str(pid).encode('utf-8'))
return pid

return old_pid

finally:
Expand Down