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.
This commit is contained in:
Brian Warner 2017-07-31 15:42:25 -07:00
parent d44c7d2c1a
commit b9d1d11b03
2 changed files with 16 additions and 18 deletions

View File

@ -2,7 +2,11 @@
# a str on Python 2 # a str on Python 2
from __future__ import print_function from __future__ import print_function
import os, time, json 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.python import log
from twisted.internet import reactor, endpoints from twisted.internet import reactor, endpoints
from twisted.application import service, internet from twisted.application import service, internet
@ -105,11 +109,10 @@ class RelayServer(service.MultiService):
self._transit_service = transit_service self._transit_service = transit_service
def increase_rlimits(self): def increase_rlimits(self):
try: if getrlimit is None:
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) log.msg("unable to import 'resource', leaving rlimit alone")
except AttributeError:
log.msg("AttributeError during getrlimit, leaving it alone")
return return
soft, hard = getrlimit(RLIMIT_NOFILE)
if soft >= 10000: if soft >= 10000:
log.msg("RLIMIT_NOFILE.soft was %d, leaving it alone" % soft) log.msg("RLIMIT_NOFILE.soft was %d, leaving it alone" % soft)
return return
@ -122,7 +125,7 @@ class RelayServer(service.MultiService):
log.msg("changing RLIMIT_NOFILE from (%s,%s) to (%s,%s)" % log.msg("changing RLIMIT_NOFILE from (%s,%s) to (%s,%s)" %
(soft, hard, newlimit, hard)) (soft, hard, newlimit, hard))
try: try:
resource.setrlimit(resource.RLIMIT_NOFILE, (newlimit, hard)) setrlimit(RLIMIT_NOFILE, (newlimit, hard))
log.msg("setrlimit successful") log.msg("setrlimit successful")
return return
except ValueError as e: except ValueError as e:

View File

@ -34,30 +34,26 @@ class RLimits(unittest.TestCase):
ep = endpoints.TCP4ServerEndpoint(None, 0) ep = endpoints.TCP4ServerEndpoint(None, 0)
with patch_s("endpoints.serverFromString", return_value=ep): with patch_s("endpoints.serverFromString", return_value=ep):
s = server.RelayServer("fake", None, None) s = server.RelayServer("fake", None, None)
noattrs = object()
fakelog = [] fakelog = []
def checklog(*expected): def checklog(*expected):
self.assertEqual(fakelog, list(expected)) self.assertEqual(fakelog, list(expected))
fakelog[:] = [] fakelog[:] = []
NF = "NOFILE" 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("log.msg", fakelog.append):
with patch_s("resource", noattrs): with patch_s("getrlimit", None):
s.increase_rlimits() s.increase_rlimits()
checklog("AttributeError during getrlimit, leaving it alone") checklog("unable to import 'resource', leaving rlimit alone")
with mock_NF: with mock_NF:
with patch_s("resource.getrlimit", with patch_s("getrlimit", return_value=(20000, 30000)) as gr:
return_value=(20000, 30000)) as gr:
s.increase_rlimits() s.increase_rlimits()
self.assertEqual(gr.mock_calls, [mock.call(NF)]) self.assertEqual(gr.mock_calls, [mock.call(NF)])
checklog("RLIMIT_NOFILE.soft was 20000, leaving it alone") checklog("RLIMIT_NOFILE.soft was 20000, leaving it alone")
with patch_s("resource.getrlimit", with patch_s("getrlimit", return_value=(10, 30000)) as gr:
return_value=(10, 30000)) as gr: with patch_s("setrlimit", side_effect=TypeError("other")):
with patch_s("resource.setrlimit",
side_effect=TypeError("other")):
with patch_s("log.err") as err: with patch_s("log.err") as err:
s.increase_rlimits() s.increase_rlimits()
self.assertEqual(err.mock_calls, [mock.call()]) self.assertEqual(err.mock_calls, [mock.call()])
@ -82,8 +78,7 @@ class RLimits(unittest.TestCase):
else: else:
expected.append("unable to change rlimit, leaving it alone") expected.append("unable to change rlimit, leaving it alone")
with patch_s("resource.setrlimit", with patch_s("setrlimit", side_effect=setrlimit) as sr:
side_effect=setrlimit) as sr:
s.increase_rlimits() s.increase_rlimits()
self.assertEqual(sr.mock_calls, calls) self.assertEqual(sr.mock_calls, calls)
checklog(*expected) checklog(*expected)