From b9d1d11b03ee77cb3a10bf986deb49a6d88ea147 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 31 Jul 2017 15:42:25 -0700 Subject: [PATCH] server.py: windows doesn't even have the 'resource' module I thought it might exist, but be empty. But it doesn't even exist on windows, so we must guard against an ImportError. --- src/wormhole/server/server.py | 15 +++++++++------ src/wormhole/test/test_server.py | 19 +++++++------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/wormhole/server/server.py b/src/wormhole/server/server.py index 8340e98..0174eca 100644 --- a/src/wormhole/server/server.py +++ b/src/wormhole/server/server.py @@ -2,7 +2,11 @@ # a str on Python 2 from __future__ import print_function import os, time, json -import resource +try: + # 'resource' is unix-only + from resource import getrlimit, setrlimit, RLIMIT_NOFILE +except ImportError: # pragma: nocover + getrlimit, setrlimit, RLIMIT_NOFILE = None, None, None # pragma: nocover from twisted.python import log from twisted.internet import reactor, endpoints from twisted.application import service, internet @@ -105,11 +109,10 @@ class RelayServer(service.MultiService): self._transit_service = transit_service def increase_rlimits(self): - try: - soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) - except AttributeError: - log.msg("AttributeError during getrlimit, leaving it alone") + if getrlimit is None: + log.msg("unable to import 'resource', leaving rlimit alone") return + soft, hard = getrlimit(RLIMIT_NOFILE) if soft >= 10000: log.msg("RLIMIT_NOFILE.soft was %d, leaving it alone" % soft) return @@ -122,7 +125,7 @@ class RelayServer(service.MultiService): log.msg("changing RLIMIT_NOFILE from (%s,%s) to (%s,%s)" % (soft, hard, newlimit, hard)) try: - resource.setrlimit(resource.RLIMIT_NOFILE, (newlimit, hard)) + setrlimit(RLIMIT_NOFILE, (newlimit, hard)) log.msg("setrlimit successful") return except ValueError as e: diff --git a/src/wormhole/test/test_server.py b/src/wormhole/test/test_server.py index 9b21b69..8f55919 100644 --- a/src/wormhole/test/test_server.py +++ b/src/wormhole/test/test_server.py @@ -34,30 +34,26 @@ class RLimits(unittest.TestCase): ep = endpoints.TCP4ServerEndpoint(None, 0) with patch_s("endpoints.serverFromString", return_value=ep): s = server.RelayServer("fake", None, None) - noattrs = object() fakelog = [] def checklog(*expected): self.assertEqual(fakelog, list(expected)) fakelog[:] = [] NF = "NOFILE" - mock_NF = patch_s("resource.RLIMIT_NOFILE", NF) + mock_NF = patch_s("RLIMIT_NOFILE", NF) with patch_s("log.msg", fakelog.append): - with patch_s("resource", noattrs): + with patch_s("getrlimit", None): s.increase_rlimits() - checklog("AttributeError during getrlimit, leaving it alone") + checklog("unable to import 'resource', leaving rlimit alone") with mock_NF: - with patch_s("resource.getrlimit", - return_value=(20000, 30000)) as gr: + with patch_s("getrlimit", return_value=(20000, 30000)) as gr: s.increase_rlimits() self.assertEqual(gr.mock_calls, [mock.call(NF)]) checklog("RLIMIT_NOFILE.soft was 20000, leaving it alone") - with patch_s("resource.getrlimit", - return_value=(10, 30000)) as gr: - with patch_s("resource.setrlimit", - side_effect=TypeError("other")): + with patch_s("getrlimit", return_value=(10, 30000)) as gr: + with patch_s("setrlimit", side_effect=TypeError("other")): with patch_s("log.err") as err: s.increase_rlimits() self.assertEqual(err.mock_calls, [mock.call()]) @@ -82,8 +78,7 @@ class RLimits(unittest.TestCase): else: expected.append("unable to change rlimit, leaving it alone") - with patch_s("resource.setrlimit", - side_effect=setrlimit) as sr: + with patch_s("setrlimit", side_effect=setrlimit) as sr: s.increase_rlimits() self.assertEqual(sr.mock_calls, calls) checklog(*expected)