Skip to content

Commit 4fe5d8f

Browse files
authored
use close_others Ruby mechanism to prevent file descriptor leaking to Python (#219)
1 parent 84cc91a commit 4fe5d8f

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
88
== Unreleased
99

1010
* stop sending/receiving `ids` between Ruby and Python
11+
* use `close_others` Ruby mechanism to prevent file descriptor leaking to Python
1112

1213
== 2.0.0.rc3 (2021-01-08) - @slonopotamus
1314

lib/pygments/mentos.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def start(self):
213213
pygmentized, this header will be followed by the text to be pygmentized.
214214
215215
The header is of form:
216-
{ "method": "highlight", "args": [], "kwargs": {"arg1": "v"}, "bytes": 128, "fd": "8"}
216+
{ "method": "highlight", "args": [], "kwargs": {"arg1": "v"}, "bytes": 128}
217217
"""
218218

219219
while True:
@@ -258,20 +258,6 @@ def main():
258258
signal.signal(signal.SIGHUP, _signal_handler)
259259

260260
mentos = Mentos()
261-
262-
if sys.platform != "win32":
263-
# close fd's inherited from the ruby parent
264-
import resource
265-
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
266-
if maxfd == resource.RLIM_INFINITY:
267-
maxfd = 65536
268-
269-
for fd in range(3, maxfd):
270-
try:
271-
os.close(fd)
272-
except:
273-
pass
274-
275261
mentos.start()
276262

277263
if __name__ == "__main__":

lib/pygments/popen.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MentosError < IOError
1414
module Pygments
1515
class Popen
1616
def popen4(argv)
17-
stdin, stdout, stderr, wait_thr = Open3.popen3(*argv)
17+
stdin, stdout, stderr, wait_thr = Open3.popen3(*argv, { close_others: true })
1818
while (pid = wait_thr.pid).nil? && wait_thr.alive?
1919
# For unknown reasons, wait_thr.pid is not immediately available on JRuby
2020
end
@@ -42,7 +42,7 @@ def start(pygments_path = File.expand_path('../../vendor/pygments-main', __dir__
4242
@pid, @in, @out, @err = popen4(argv)
4343
@in.binmode
4444
@out.binmode
45-
@log.info "Starting pid #{@pid} with fd #{@out.to_i} and python #{python_binary}."
45+
@log.info "Starting pid #{@pid} with python #{python_binary}."
4646
end
4747

4848
def python_binary
@@ -280,7 +280,7 @@ def mentos(method, args = [], kwargs = {}, code = nil)
280280
end
281281

282282
kwargs.freeze
283-
kwargs = kwargs.merge('fd' => @out.to_i, 'bytes' => bytesize)
283+
kwargs = kwargs.merge('bytes' => bytesize)
284284
out_header = JSON.generate(method: method, args: args, kwargs: kwargs)
285285

286286
begin

0 commit comments

Comments
 (0)