mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) communicate with sandbox via standard pipes
Summary: This switches to using stdin/stdout for RPC calls to the sandbox, rather than specially allocated side channels. Plain text error information remains on stderr. The motivation for the change is to simplify use of sandboxes, some of which support extra file descriptors and some of which don't. The new style of communication is made the default, but I'm not committed to this, just that it be easy to switch to if needed. It is possible I'll need to switch the communication method again in the near future. One reason not to make this default would be windows support, which is likely broken since stdin/stdout are by default in text mode. Test Plan: existing tests pass Reviewers: dsagal, alexmojaki Reviewed By: dsagal, alexmojaki Differential Revision: https://phab.getgrist.com/D2897
This commit is contained in:
@@ -13,7 +13,7 @@ import six
|
||||
|
||||
from acl_formula import parse_acl_formula
|
||||
import actions
|
||||
from sandbox import Sandbox
|
||||
from sandbox import get_default_sandbox
|
||||
import engine
|
||||
import migrations
|
||||
import schema
|
||||
@@ -115,8 +115,7 @@ def run(sandbox):
|
||||
sandbox.run()
|
||||
|
||||
def main():
|
||||
sandbox = Sandbox.connected_to_js_pipes()
|
||||
run(sandbox)
|
||||
run(get_default_sandbox())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -42,10 +42,26 @@ class Sandbox(object):
|
||||
|
||||
@classmethod
|
||||
def connected_to_js_pipes(cls):
|
||||
"""
|
||||
Send data on two specially-opened side channels.
|
||||
"""
|
||||
external_input = os.fdopen(3, "rb", 64 * 1024)
|
||||
external_output = os.fdopen(4, "wb", 64 * 1024)
|
||||
return cls(external_input, external_output)
|
||||
|
||||
@classmethod
|
||||
def use_common_pipes(cls):
|
||||
"""
|
||||
Send data via stdin/stdout, rather than specially-opened side channels.
|
||||
Duplicate stdin/stdout, close, and reopen as binary file objects.
|
||||
"""
|
||||
os.dup2(0, 3)
|
||||
os.dup2(1, 4)
|
||||
os.close(0)
|
||||
os.close(1)
|
||||
sys.stdout = sys.stderr
|
||||
return Sandbox.connected_to_js_pipes()
|
||||
|
||||
def _send_to_js(self, msgCode, msgBody):
|
||||
# (Note that marshal version 2 is the default; we specify it explicitly for clarity. The
|
||||
# difference with version 0 is that version 2 uses a faster binary format for floats.)
|
||||
@@ -97,7 +113,10 @@ default_sandbox = None
|
||||
def get_default_sandbox():
|
||||
global default_sandbox
|
||||
if default_sandbox is None:
|
||||
default_sandbox = Sandbox.connected_to_js_pipes()
|
||||
if os.environ.get('PIPE_MODE') == 'minimal':
|
||||
default_sandbox = Sandbox.use_common_pipes()
|
||||
else:
|
||||
default_sandbox = Sandbox.connected_to_js_pipes()
|
||||
return default_sandbox
|
||||
|
||||
def call_external(name, *args):
|
||||
|
||||
Reference in New Issue
Block a user