99import threading
1010import warnings
1111from copy import copy
12+ from contextlib import contextmanager
1213
1314import requests
1415import retrying
@@ -836,6 +837,36 @@ def __repr__(self):
836837del OrcaStatus
837838
838839
840+ @contextmanager
841+ def orca_env ():
842+ """
843+ Context manager to clear and restore environment variables that are
844+ problematic for orca to function properly
845+
846+ NODE_OPTIONS: When this variable is set, orca <v1.2 will have a
847+ segmentation fault due to an electron bug.
848+ See: https://github.com/electron/electron/issues/12695
849+
850+ ELECTRON_RUN_AS_NODE: When this environment variable is set the call
851+ to orca is transformed into a call to nodejs.
852+ See https://github.com/plotly/orca/issues/149#issuecomment-443506732
853+ """
854+ clear_env_vars = ['NODE_OPTIONS' , 'ELECTRON_RUN_AS_NODE' ]
855+ orig_env_vars = {}
856+
857+ try :
858+ # Clear and save
859+ orig_env_vars .update ({
860+ var : os .environ .pop (var )
861+ for var in clear_env_vars
862+ if var in os .environ })
863+ yield
864+ finally :
865+ # Restore
866+ for var , val in orig_env_vars .items ():
867+ os .environ [var ] = val
868+
869+
839870# Public orca server interaction functions
840871# ----------------------------------------
841872def validate_executable ():
@@ -918,13 +949,6 @@ def validate_executable():
918949 formatted_path = formatted_path ,
919950 instructions = install_location_instructions ))
920951
921- # Clear NODE_OPTIONS environment variable
922- # ---------------------------------------
923- # When this variable is set, orca <v1.2 will have a segmentation fault
924- # due to an electron bug.
925- # See: https://github.com/electron/electron/issues/12695
926- os .environ .pop ('NODE_OPTIONS' , None )
927-
928952 # Run executable with --help and see if it's our orca
929953 # ---------------------------------------------------
930954 invalid_executable_msg = """
@@ -938,12 +962,13 @@ def validate_executable():
938962 instructions = install_location_instructions )
939963
940964 # ### Run with Popen so we get access to stdout and stderr
941- p = subprocess .Popen (
942- [executable , '--help' ],
943- stdout = subprocess .PIPE ,
944- stderr = subprocess .PIPE )
965+ with orca_env ():
966+ p = subprocess .Popen (
967+ [executable , '--help' ],
968+ stdout = subprocess .PIPE ,
969+ stderr = subprocess .PIPE )
945970
946- help_result , help_error = p .communicate ()
971+ help_result , help_error = p .communicate ()
947972
948973 if p .returncode != 0 :
949974 err_msg = invalid_executable_msg + """
@@ -986,12 +1011,13 @@ def validate_executable():
9861011 # Get orca version
9871012 # ----------------
9881013 # ### Run with Popen so we get access to stdout and stderr
989- p = subprocess .Popen (
990- [executable , '--version' ],
991- stdout = subprocess .PIPE ,
992- stderr = subprocess .PIPE )
1014+ with orca_env ():
1015+ p = subprocess .Popen (
1016+ [executable , '--version' ],
1017+ stdout = subprocess .PIPE ,
1018+ stderr = subprocess .PIPE )
9931019
994- version_result , version_error = p .communicate ()
1020+ version_result , version_error = p .communicate ()
9951021
9961022 if p .returncode != 0 :
9971023 raise ValueError (invalid_executable_msg + """
@@ -1171,8 +1197,9 @@ def ensure_server():
11711197 # Create subprocess that launches the orca server on the
11721198 # specified port.
11731199 DEVNULL = open (os .devnull , 'wb' )
1174- orca_state ['proc' ] = subprocess .Popen (cmd_list ,
1175- stdout = DEVNULL )
1200+ with orca_env ():
1201+ orca_state ['proc' ] = subprocess .Popen (cmd_list ,
1202+ stdout = DEVNULL )
11761203
11771204 # Update orca.status so the user has an accurate view
11781205 # of the state of the orca server
@@ -1191,7 +1218,7 @@ def ensure_server():
11911218 orca_state ['shutdown_timer' ] = t
11921219
11931220
1194- @retrying .retry (wait_random_min = 5 , wait_random_max = 10 , stop_max_delay = 8000 )
1221+ @retrying .retry (wait_random_min = 5 , wait_random_max = 10 , stop_max_delay = 30000 )
11951222def request_image_with_retrying (** kwargs ):
11961223 """
11971224 Helper method to perform an image request to a running orca server process
0 commit comments