Skip to content

use close_others Ruby mechanism to prevent file descriptor leaking to Python #219

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

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
== Unreleased

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

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

Expand Down
16 changes: 1 addition & 15 deletions lib/pygments/mentos.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def start(self):
pygmentized, this header will be followed by the text to be pygmentized.

The header is of form:
{ "method": "highlight", "args": [], "kwargs": {"arg1": "v"}, "bytes": 128, "fd": "8"}
{ "method": "highlight", "args": [], "kwargs": {"arg1": "v"}, "bytes": 128}
"""

while True:
Expand Down Expand Up @@ -258,20 +258,6 @@ def main():
signal.signal(signal.SIGHUP, _signal_handler)

mentos = Mentos()

if sys.platform != "win32":
# close fd's inherited from the ruby parent
import resource
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if maxfd == resource.RLIM_INFINITY:
maxfd = 65536

for fd in range(3, maxfd):
try:
os.close(fd)
except:
pass

mentos.start()

if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions lib/pygments/popen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MentosError < IOError
module Pygments
class Popen
def popen4(argv)
stdin, stdout, stderr, wait_thr = Open3.popen3(*argv)
stdin, stdout, stderr, wait_thr = Open3.popen3(*argv, { close_others: true })
while (pid = wait_thr.pid).nil? && wait_thr.alive?
# For unknown reasons, wait_thr.pid is not immediately available on JRuby
end
Expand Down Expand Up @@ -42,7 +42,7 @@ def start(pygments_path = File.expand_path('../../vendor/pygments-main', __dir__
@pid, @in, @out, @err = popen4(argv)
@in.binmode
@out.binmode
@log.info "Starting pid #{@pid} with fd #{@out.to_i} and python #{python_binary}."
@log.info "Starting pid #{@pid} with python #{python_binary}."
end

def python_binary
Expand Down Expand Up @@ -280,7 +280,7 @@ def mentos(method, args = [], kwargs = {}, code = nil)
end

kwargs.freeze
kwargs = kwargs.merge('fd' => @out.to_i, 'bytes' => bytesize)
kwargs = kwargs.merge('bytes' => bytesize)
out_header = JSON.generate(method: method, args: args, kwargs: kwargs)

begin
Expand Down