Skip to content

Commit 058ca3f

Browse files
committed
Add thread safety and explicit lifetimes for system resources
Signed-off-by: Joe Friedrichsen <[email protected]>
1 parent 0f41a19 commit 058ca3f

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/nipanel/streamlit_refresh/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import threading
6+
57
from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient
68
from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool
79
from streamlit.components.v1 import declare_component
@@ -24,13 +26,15 @@ def initialize_refresh_component(panel_id: str) -> CustomComponent:
2426

2527

2628
def _get_or_resolve_proxy() -> str:
27-
global _panel_service_proxy_location
28-
if _panel_service_proxy_location is None:
29-
discovery_client = DiscoveryClient(grpc_channel_pool=GrpcChannelPool())
30-
service_location = discovery_client.resolve_service(
31-
provided_interface="ni.http1.proxy",
32-
service_class="",
33-
)
34-
_panel_service_proxy_location = service_location.insecure_address
35-
36-
return _panel_service_proxy_location
29+
with threading.RLock():
30+
global _panel_service_proxy_location
31+
if _panel_service_proxy_location is None:
32+
with GrpcChannelPool() as grpc_channel_pool:
33+
discovery_client = DiscoveryClient(grpc_channel_pool=grpc_channel_pool)
34+
service_location = discovery_client.resolve_service(
35+
provided_interface="ni.http1.proxy",
36+
service_class="",
37+
)
38+
_panel_service_proxy_location = service_location.insecure_address
39+
40+
return _panel_service_proxy_location

0 commit comments

Comments
 (0)