Skip to content

Commit

Permalink
Fix port in use when starting up and shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni-SM committed Sep 3, 2022
1 parent 64529dc commit aa1f44d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions exts/semu.misc.vscode/config/extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ name = "semu.misc.vscode"
exts."semu.misc.vscode".socket_ip = "0.0.0.0"
exts."semu.misc.vscode".socket_port = 8226
exts."semu.misc.vscode".carb_logging = true
exts."semu.misc.vscode".kill_processes_with_port_in_use = true
27 changes: 26 additions & 1 deletion exts/semu.misc.vscode/semu/misc/vscode/scripts/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import threading
import traceback
import contextlib
import subprocess
from io import StringIO
from dis import COMPILER_FLAG_NAMES
try:
Expand Down Expand Up @@ -108,14 +109,34 @@ def on_startup(self, ext_id):
self._socket_ip = self._settings.get("/exts/semu.misc.vscode/socket_ip")
self._socket_port = self._settings.get("/exts/semu.misc.vscode/socket_port")
self._carb_logging = self._settings.get("/exts/semu.misc.vscode/carb_logging")
self._socket_last_error = ""
kill_processes_with_port_in_use = self._settings.get("/exts/semu.misc.vscode/kill_processes_with_port_in_use")

# menu item
self._editor_menu = omni.kit.ui.get_editor_menu()
if self._editor_menu:
self._menu = self._editor_menu.add_item(Extension.MENU_PATH, self._show_notification, toggle=False, value=False)

# shutdown stream
self.shutdown_stream_ebent = omni.kit.app.get_app().get_shutdown_event_stream() \
.create_subscription_to_pop(self._on_shutdown_event, name="semu.misc.vscode", order=0)

# ensure port is free
if kill_processes_with_port_in_use:
if sys.platform == "win32":
pids = []
cmd = ["netstat", "-ano"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in p.stdout:
if str(self._socket_port).encode() in line:
pids.append(line.strip().split(b" ")[-1].decode())
p.wait()
for pid in pids:
carb.log_warn(f"Forced process shutdown with PID {pid}")
cmd = ["taskkill", "/PID", pid, "/F"]
subprocess.Popen(cmd).wait()

# create socket
self._socket_last_error = ""
self._server = None
self._create_socket()

Expand Down Expand Up @@ -187,6 +208,10 @@ def on_shutdown(self):

# extension ui methods

def _on_shutdown_event(self, event):
if event.type == omni.kit.app.POST_QUIT_EVENT_TYPE:
self.on_shutdown()

def _show_notification(self, *args, **kwargs) -> None:
"""Show extension data in the notification area
"""
Expand Down

0 comments on commit aa1f44d

Please sign in to comment.