9
9
import threading
10
10
import warnings
11
11
from copy import copy
12
+ from contextlib import contextmanager
12
13
13
14
import requests
14
15
import retrying
@@ -836,6 +837,36 @@ def __repr__(self):
836
837
del OrcaStatus
837
838
838
839
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
+
839
870
# Public orca server interaction functions
840
871
# ----------------------------------------
841
872
def validate_executable ():
@@ -918,13 +949,6 @@ def validate_executable():
918
949
formatted_path = formatted_path ,
919
950
instructions = install_location_instructions ))
920
951
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
-
928
952
# Run executable with --help and see if it's our orca
929
953
# ---------------------------------------------------
930
954
invalid_executable_msg = """
@@ -938,12 +962,13 @@ def validate_executable():
938
962
instructions = install_location_instructions )
939
963
940
964
# ### 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 )
945
970
946
- help_result , help_error = p .communicate ()
971
+ help_result , help_error = p .communicate ()
947
972
948
973
if p .returncode != 0 :
949
974
err_msg = invalid_executable_msg + """
@@ -986,12 +1011,13 @@ def validate_executable():
986
1011
# Get orca version
987
1012
# ----------------
988
1013
# ### 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 )
993
1019
994
- version_result , version_error = p .communicate ()
1020
+ version_result , version_error = p .communicate ()
995
1021
996
1022
if p .returncode != 0 :
997
1023
raise ValueError (invalid_executable_msg + """
@@ -1171,8 +1197,9 @@ def ensure_server():
1171
1197
# Create subprocess that launches the orca server on the
1172
1198
# specified port.
1173
1199
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 )
1176
1203
1177
1204
# Update orca.status so the user has an accurate view
1178
1205
# of the state of the orca server
@@ -1191,7 +1218,7 @@ def ensure_server():
1191
1218
orca_state ['shutdown_timer' ] = t
1192
1219
1193
1220
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 )
1195
1222
def request_image_with_retrying (** kwargs ):
1196
1223
"""
1197
1224
Helper method to perform an image request to a running orca server process
0 commit comments