Skip to content

Commit e3f54d0

Browse files
authored
Merge pull request #201 from minrk/sticky-connection-file
set sticky bit on connection files
2 parents b209e1b + 63e5d4c commit e3f54d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

jupyter_client/connect.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import json
1515
import os
1616
import socket
17+
import stat
1718
import tempfile
1819
import warnings
1920
from getpass import getpass
@@ -135,6 +136,27 @@ def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0,
135136
with open(fname, 'w') as f:
136137
f.write(json.dumps(cfg, indent=2))
137138

139+
if hasattr(stat, 'S_ISVTX'):
140+
# set the sticky bit on the file and its parent directory
141+
# to avoid periodic cleanup
142+
paths = [fname]
143+
runtime_dir = os.path.dirname(fname)
144+
if runtime_dir:
145+
paths.append(runtime_dir)
146+
for path in paths:
147+
permissions = os.stat(path).st_mode
148+
new_permissions = permissions | stat.S_ISVTX
149+
if new_permissions != permissions:
150+
try:
151+
os.chmod(path, permissions)
152+
except OSError as e:
153+
# failed to set sticky bit,
154+
# probably not a big deal
155+
warnings.warn(
156+
"Failed to set sticky bit on %r: %s" % (path, e),
157+
RuntimeWarning,
158+
)
159+
138160
return fname, cfg
139161

140162

0 commit comments

Comments
 (0)