Skip to content

Commit

Permalink
Remove broken memory limit feature
Browse files Browse the repository at this point in the history
  • Loading branch information
citrus-it committed Dec 30, 2024
1 parent 98f2225 commit 481e05a
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 112 deletions.
21 changes: 0 additions & 21 deletions src/modules/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3212,27 +3212,6 @@ def cmp(a, b):
return NotImplemented


def set_memory_limit(bytes, allow_override=True):
"""Limit memory consumption of current process to 'bytes'.
This only limits the brk() and mmap() calls made by python."""

if allow_override:
try:
bytes = int(os.environ["PKG_CLIENT_MAX_PROCESS_SIZE"])
except (KeyError, ValueError):
pass

try:
resource.setrlimit(resource.RLIMIT_VMEM, (bytes, bytes))
except AttributeError:
# If platform doesn't support RLIMIT_VMEM, just ignore it.
pass
except ValueError:
# An unprivileged user can not raise a previously set limit,
# if that ever happens, just ignore it.
pass


def force_bytes(s, encoding="utf-8", errors="strict"):
"""Force the string into bytes."""

Expand Down
3 changes: 0 additions & 3 deletions src/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,6 @@ def main_func():

temp_root = misc.config_temp_root()

# set process limits for memory consumption to 8GB
misc.set_memory_limit(8 * 1024 * 1024 * 1024)

global_settings.client_name = "pkgrecv"
target = os.environ.get("PKG_DEST", None)
src_uri = os.environ.get("PKG_SRC", None)
Expand Down
88 changes: 0 additions & 88 deletions src/tests/api/t_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,94 +115,6 @@ def test_psinfo(self):
libc = ctypes.CDLL("libc.so")
self.assertEqual(psinfo.pr_zoneid, libc.getzoneid())

def test_memory_limit(self):
"""Verify that set_memory_limit works."""

# memory limit to test, keep small to avoid test slowdown
mem_cap = 100 * 1024 * 1024
# measured process resources. Note, that we have a static
# overhead in waste.py for the forking of ps, so while 20M seems
# large compared to a 100M limit, in a real world example with
# 8G limit it's fairly small.
mem_tol = 20 * 1024 * 1024

waste_mem_py = """
import os
import resource
import subprocess
import pkg.misc as misc
misc.set_memory_limit({0})
i = 0
x = {{}}
try:
while True:
i += 1
x[i] = range(i)
except MemoryError:
# give us some breathing room (enough so the test with env var works)
misc.set_memory_limit({0} * 3, allow_override=False)
print(subprocess.check_output(['ps', '-o', 'rss=', '-p',
str(os.getpid())], universal_newlines=True).strip())
""".format(
str(mem_cap)
)

# Re-setting limits which are higher than original limit can
# only be done by root.
self.assertTrue(os.geteuid() == 0, "must be root to run this test")

tmpdir = tempfile.mkdtemp(dir=self.test_root)
tmpfile = os.path.join(tmpdir, "waste.py")
with open(tmpfile, "w") as f:
f.write(waste_mem_py)

pyv = ".".join(platform.python_version_tuple()[:2])

res = int(subprocess.check_output([f"python{pyv}", tmpfile]))
# convert from kB to bytes
res *= 1024

self.debug("mem_cap: " + str(mem_cap))
self.debug("proc size: " + str(res))

self.assertTrue(
res < mem_cap + mem_tol, "process mem consumption too high"
)
self.assertTrue(
res > mem_cap - mem_tol, "process mem consumption too low"
)

# test if env var works
os.environ["PKG_CLIENT_MAX_PROCESS_SIZE"] = str(mem_cap * 2)
res = int(subprocess.check_output([f"python{pyv}", tmpfile]))
res *= 1024

self.debug("mem_cap: " + str(mem_cap))
self.debug("proc size: " + str(res))

self.assertTrue(
res < mem_cap * 2 + mem_tol, "process mem consumption too high"
)
# self.assertTrue(res > mem_cap * 2 - mem_tol,
# "process mem consumption too low")

# test if invalid env var is handled correctly
os.environ["PKG_CLIENT_MAX_PROCESS_SIZE"] = "octopus"
res = int(subprocess.check_output([f"python{pyv}", tmpfile]))
res *= 1024

self.debug("mem_cap: " + str(mem_cap))
self.debug("proc size: " + str(res))

self.assertTrue(
res < mem_cap + mem_tol, "process mem consumption too high"
)
self.assertTrue(
res > mem_cap - mem_tol, "process mem consumption too low"
)


if __name__ == "__main__":
unittest.main()
Expand Down

0 comments on commit 481e05a

Please sign in to comment.