Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions klippy/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ def main():
logging.getLogger().setLevel(debuglevel)
logging.info("=======================")
logging.info("Starting Klippy...")
util.lock_memory()
git_info = util.get_git_version()
git_vers = git_info["version"]

Expand Down
20 changes: 20 additions & 0 deletions klippy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import ctypes
import ctypes.util
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctypes.util is imported but never used. Either remove this import to avoid lint/noise, or use it to resolve the libc path (e.g., via ctypes.util.find_library).

Suggested change
import ctypes.util

Copilot uses AI. Check for mistakes.
import fcntl
import json
import logging
Expand All @@ -27,6 +29,24 @@ def fix_sigint():
fix_sigint()


# Lock all current and future memory pages into RAM to prevent swapping
def lock_memory():
MCL_CURRENT = 1
MCL_FUTURE = 2
try:
libc = ctypes.CDLL("libc.so.6", use_errno=True)
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading libc via the hard-coded name libc.so.6 is glibc-specific and can fail on non-glibc systems (e.g., musl-based containers). Consider using ctypes.util.find_library('c') (with a sensible fallback) or ctypes.CDLL(None) to locate mlockall more portably.

Suggested change
libc = ctypes.CDLL("libc.so.6", use_errno=True)
libc = None
for libname in (None, ctypes.util.find_library("c"), "libc.so.6"):
if libname is None:
candidate = ctypes.CDLL(None, use_errno=True)
else:
try:
candidate = ctypes.CDLL(libname, use_errno=True)
except OSError:
continue
if hasattr(candidate, "mlockall"):
libc = candidate
break
if libc is None:
raise OSError("Unable to locate libc with mlockall")

Copilot uses AI. Check for mistakes.
result = libc.mlockall(MCL_CURRENT | MCL_FUTURE)
if result != 0:
errno_val = ctypes.get_errno()
raise OSError(errno_val, os.strerror(errno_val))
logging.info(
"Memory locked to RAM (mlockall MCL_CURRENT|MCL_FUTURE):"
" swapping disabled"
)
except Exception as e:
logging.warning("Failed to lock memory: %s", e)


# Set a file-descriptor as non-blocking
def set_nonblock(fd):
fcntl.fcntl(
Expand Down
Loading