Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def kill_plugin(engine, pid):
"""Kill plugin."""
logger = engine.logger
plugins = engine.store.plugins
plugin_sids = engine.store.plugin_sids
plugin_signatures = engine.store.plugin_signatures
if pid in plugins:
try:
plugins[pid]["abort"].set()
plugins[pid]["aborting"] = asyncio.get_event_loop().create_future()
if plugins[pid]["process_id"] is not None:
kill_process(plugins[pid]["process_id"], logger)
except Exception as exc: # pylint: disable=broad-except
logger.error("Failed to kill plugin %s, error: %s", pid, exc)
if "sid" in plugins[pid]:
if plugins[pid]["sid"] in plugin_sids:
del plugin_sids[plugins[pid]["sid"]]
if plugins[pid]["signature"] in plugin_signatures:
logger.info(
"Cleaning up killed plugin signature %s", plugins[pid]["signature"]
)
del plugin_signatures[plugins[pid]["signature"]]
logger.info("Cleaning up killed plugin %s", pid)
del plugins[pid]
plugins = engine.store.plugins
registered_sessions = engine.store.registered_sessions
if sid not in registered_sessions:
logger.debug("Client %s is not registered", sid)
return {"success": False, "error": "client has not been registered."}
if "all" not in kwargs:
return {
"success": False,
"error": 'You must provide the pid of the plugin process or "all=true".',
}
if kwargs["all"]:
logger.info("Killing all the plugins")
await kill_all_plugins(engine, sid)
return {"success": True}
try:
kill_process(int(kwargs["pid"]), logger)
return {"success": True}
except Exception: # pylint: disable=broad-except
return {
"success": False,
"error": "Failed to kill plugin process: #" + str(kwargs["pid"]),
}
psutil = get_psutil()
if not psutil:
return
current_process = psutil.Process()
children = current_process.children(recursive=True)
pids = []
for proc in children:
if proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE:
pids.append(proc.pid)
def loop(): # executed in another thread
for i in range(5):
logger.info("Exiting: %s", 5 - i)
time.sleep(0.5)
if stopped.is_set():
break
logger.debug("Plugin engine is killed")
kill_process(os.getpid(), logger)
)
opt.workspace_dir = os.path.expanduser(opt.workspace)
if not os.path.exists(opt.workspace_dir):
os.makedirs(opt.workspace_dir)
if not opt.token:
opt.token = read_or_generate_token(os.path.join(opt.workspace_dir, ".token"))
# try to kill last process
pid_file = os.path.join(opt.workspace_dir, ".pid")
try:
if os.path.exists(pid_file):
with open(pid_file, "r") as fil:
pid = int(fil.read())
logger.info("Trying to kill last process (pid=%s)", pid)
kill_process(pid, logger)
# wait for a while to release the port
time.sleep(3)
except Exception: # pylint: disable=broad-except
logger.debug("Failed to kill last process")
try:
engine_pid = str(os.getpid())
with open(pid_file, "w") as fil:
fil.write(engine_pid)
except Exception as exc: # pylint: disable=broad-except
logger.error("Failed to save .pid file: %s", str(exc))
opt.web_app_dir = os.path.join(opt.workspace_dir, "__ImJoy__")
if opt.serve:
if shutil.which("git") is None:
logger.info("Installing git")
progress = 0
logging_callback(progress, type="progress")
while True:
out = process.stdout.read(1)
if out == "" and process.poll() is not None:
break
os.write(stdfn, out)
sys.stdout.flush()
if abort.is_set() or process.poll() is not None:
break
time.sleep(0)
logger.info("Plugin aborting")
kill_process(process.pid, logger)
outputs, errors = process.communicate()
if outputs is not None:
outputs = str(outputs, "utf-8")
if errors is not None:
errors = str(errors, "utf-8")
exit_code = process.returncode
except Exception as exc: # pylint: disable=broad-except
logger.error(traceback.format_exc())
outputs, errors = "", str(exc)
exit_code = 100
if exit_code == 0:
logging_callback(f"Plugin process exited with code {exit_code}")
stop_callback(True, outputs)
return True