Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Handling of the soft limit on open file handles #376

Merged
merged 1 commit into from
Jan 7, 2021
Merged
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
48 changes: 48 additions & 0 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@

from types import ModuleType
from base64 import encodebytes
try:
import resource
except ImportError:
# Windows
resource = None

from jinja2 import Environment, FileSystemLoader

from jupyter_server.transutils import trans, _
Expand Down Expand Up @@ -758,6 +764,30 @@ def _token_default(self):
self._token_generated = True
return binascii.hexlify(os.urandom(24)).decode('ascii')

min_open_files_limit = Integer(config=True,
help="""
Gets or sets a lower bound on the open file handles process resource
limit. This may need to be increased if you run into an
OSError: [Errno 24] Too many open files.
This is not applicable when running on Windows.
""")

@default('min_open_files_limit')
def _default_min_open_files_limit(self):
if resource is None:
# Ignoring min_open_files_limit because the limit cannot be adjusted (for example, on Windows)
return None

soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)

DEFAULT_SOFT = 4096
if hard >= DEFAULT_SOFT:
return DEFAULT_SOFT

self.log.debug("Default value for min_open_files_limit is ignored (hard=%r, soft=%r)", hard, soft)

return soft

max_body_size = Integer(512 * 1024 * 1024, config=True,
help="""
Sets the maximum allowed size of the client request body, specified in
Expand Down Expand Up @@ -1395,6 +1425,23 @@ def init_webapp(self):

self.login_handler_class.validate_security(self, ssl_options=self.ssl_options)

def init_resources(self):
"""initialize system resources"""
if resource is None:
self.log.debug('Ignoring min_open_files_limit because the limit cannot be adjusted (for example, on Windows)')
return

old_soft, old_hard = resource.getrlimit(resource.RLIMIT_NOFILE)
soft = self.min_open_files_limit
hard = old_hard
if old_soft < soft:
if hard < soft:
hard = soft
self.log.debug(
'Raising open file limit: soft {}->{}; hard {}->{}'.format(old_soft, soft, old_hard, hard)
)
resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))

@property
def display_url(self):
if self.custom_display_url:
Expand Down Expand Up @@ -1712,6 +1759,7 @@ def initialize(self, argv=None, find_extensions=True, new_httpserver=True):
self.find_server_extensions()
self.init_logging()
self.init_server_extensions()
self.init_resources()
self.init_configurables()
self.init_components()
self.init_webapp()
Expand Down