Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

## Unreleased

### A setting chosen while the Manager menu is loading it is kept

The Manager menu asks the server for the database mode, the component policy,
the update policy and the share option when it is first opened. A value chosen
before the answer arrived was saved, but the menu went back to showing the old
one. The menu keeps the chosen value.

### Setting requests with an unknown value are refused

The routes that set `db_mode`, `update_policy` and `component_policy` stored
whatever value a request carried and wrote it to `config.ini`. They answer 400
for a value that is not one of those the Manager's menu offers, and for a JSON
body without a `value`, which ended with status 500.

### Install requests with an unusable body are answered with 400

`/customnode/install/git_url` and `/customnode/install/pip` answered 500 when
the JSON body was not an object, or when `url` or `packages` was not a string.
They answer 400, as they do for a missing field.

### Console messages show paths, URLs and package names as they are

Manager printed many of its messages through rich, which reads square
brackets as markup. A package name such as `diffusers[torch]` was shown as
`diffusers`, a text such as `:100:` as an emoji, and a line longer than the
console was broken in the middle of a path or a URL. A text with a closing
tag such as `[/b]` raised `MarkupError` instead of being printed, in the
middle of the operation that reported it. These messages are now printed as
they are; numbers and quoted strings keep their colors.

### The list of ComfyUI versions answers a failure with 400 again

When the versions of ComfyUI could not be read, the request for the list ended
with status 500 and a `TypeError` in the log: the call that logs the cause was
given an argument it does not take. The cause is logged, and the answer is 400.

### Restarting under comfy-cli no longer reports a closed log file

Restarting ComfyUI from the Manager under comfy-cli printed `ValueError: I/O
Expand Down
12 changes: 10 additions & 2 deletions glob/manager_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

orig_print = print

from rich import print
import rich
from rich.console import Console
from rich.markup import escape
from packaging import version

import uuid
Expand All @@ -44,6 +46,12 @@
from node_package import InstalledNodePackage


def print(*objects, sep=" ", end="\n", file=None):
# rich's print with its highlighting only, so that a path, a URL or an error text is written as it is
console = rich.get_console() if file is None else Console(file=file)
console.print(*objects, sep=sep, end=end, markup=False, emoji=False, soft_wrap=True)


version_code = [3, 43]
version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '')

Expand Down Expand Up @@ -773,7 +781,7 @@ async def load_nightly(channel, mode):
channel_url = normalize_channel(channel)
if channel_url:
if mode not in ['remote', 'local', 'cache']:
print(f"[bold red]ERROR: Invalid mode is specified `--mode {mode}`[/bold red]", file=sys.stderr)
rich.print(f"[bold red]ERROR: Invalid mode is specified `--mode {escape(mode)}`[/bold red]", file=sys.stderr)
return {}

# validate channel - only the channel set by the user is allowed.
Expand Down
58 changes: 44 additions & 14 deletions glob/manager_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,24 @@ def set_preview_method(method):
)


ALLOWED_COMPONENT_POLICIES = ('workflow', 'higher', 'mine')
ALLOWED_UPDATE_POLICIES = ('stable-comfyui', 'nightly-comfyui')
ALLOWED_DB_MODES = ('cache', 'local', 'remote')


def set_component_policy(mode):
if mode not in ALLOWED_COMPONENT_POLICIES:
raise ValueError(f"Invalid component_policy {mode!r}; must be one of {ALLOWED_COMPONENT_POLICIES}")
core.get_config()['component_policy'] = mode

def set_update_policy(mode):
if mode not in ALLOWED_UPDATE_POLICIES:
raise ValueError(f"Invalid update_policy {mode!r}; must be one of {ALLOWED_UPDATE_POLICIES}")
core.get_config()['update_policy'] = mode

def set_db_mode(mode):
if mode not in ALLOWED_DB_MODES:
raise ValueError(f"Invalid db_mode {mode!r}; must be one of {ALLOWED_DB_MODES}")
core.get_config()['db_mode'] = mode

def print_comfyui_version():
Expand Down Expand Up @@ -1297,7 +1308,7 @@ def unzip_install(files):

os.remove(temp_filename)
except Exception as e:
logging.error(f"Install(unzip) error: {url} / {e}", file=sys.stderr)
logging.error(f"Install(unzip) error: {url} / {e}")
return False

logging.info("Installation was successful.")
Expand All @@ -1319,7 +1330,7 @@ def copy_install(files, js_path_name=None):
download_url(url, path, filename)

except Exception as e:
logging.error(f"Install(copy) error: {url} / {e}", file=sys.stderr)
logging.error(f"Install(copy) error: {url} / {e}")
return False

logging.info("Installation was successful.")
Expand All @@ -1340,7 +1351,7 @@ def copy_uninstall(files, js_path_name='.'):
elif os.path.exists(file_path + ".disabled"):
os.remove(file_path + ".disabled")
except Exception as e:
logging.error(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr)
logging.error(f"Uninstall(copy) error: {url} / {e}")
return False

logging.info("Uninstallation was successful.")
Expand Down Expand Up @@ -1371,7 +1382,7 @@ def copy_set_active(files, is_disable, js_path_name='.'):
os.rename(current_name, new_name)

except Exception as e:
logging.error(f"{action_name}(copy) error: {url} / {e}", file=sys.stderr)
logging.error(f"{action_name}(copy) error: {url} / {e}")

return False

Expand Down Expand Up @@ -1601,12 +1612,14 @@ async def install_custom_node_git_url(request):
# cannot forge an application/json body without a CORS preflight, which this
# server does not answer — same body-handler convention as every other
# request.json() route (see _reject_simple_form_content_type docstring).
# A malformed body or a missing 'url' is a client error (400), not a 500:
# don't let JSONDecodeError/KeyError bubble up as a server fault.
# A malformed body, a missing 'url' or one that is not a string is a client
# error (400), not a 500: don't let it bubble up as a server fault.
try:
json_data = await request.json()
url = json_data['url']
except (KeyError, ValueError):
except (KeyError, TypeError, ValueError):
url = None
if not isinstance(url, str):
return web.Response(status=400, text="Invalid request body: expected JSON object with a 'url' field")
res = await core.gitclone_install(url)

Expand All @@ -1628,12 +1641,14 @@ async def install_custom_node_pip(request):
return security_403_response(flag_token='allow_pip_install')

# JSON body (not raw text) for the same preflight-forcing reason as
# /customnode/install/git_url above; malformed body or missing 'packages'
# is a 400, not a 500.
# /customnode/install/git_url above; malformed body, missing 'packages' or
# one that is not a string is a 400, not a 500.
try:
json_data = await request.json()
packages = json_data['packages']
except (KeyError, ValueError):
except (KeyError, TypeError, ValueError):
packages = None
if not isinstance(packages, str):
return web.Response(status=400, text="Invalid request body: expected JSON object with a 'packages' field")
core.pip_install(packages.split(' '))

Expand Down Expand Up @@ -1700,7 +1715,7 @@ async def comfyui_versions(request):
res, current, latest = core.get_comfyui_versions()
return web.json_response({'versions': res, 'current': current}, status=200, content_type='application/json')
except Exception as e:
logging.error(f"ComfyUI update fail: {e}", file=sys.stderr)
logging.error(f"ComfyUI update fail: {e}")

return web.Response(status=400)

Expand Down Expand Up @@ -1812,7 +1827,12 @@ async def get_db_mode(request):
@routes.post("/manager/db_mode")
async def set_db_mode_handler(request):
json_data = await request.json()
set_db_mode(json_data['value'])
try:
Comment thread
ltdrdata marked this conversation as resolved.
Resolved
set_db_mode(json_data['value'])
except (KeyError, TypeError):
return web.Response(status=400, text='Invalid request')
except ValueError as e:
return web.Response(status=400, text=str(e))
core.write_config()
return web.Response(status=200)

Expand All @@ -1825,7 +1845,12 @@ async def get_component_policy(request):
@routes.post("/manager/policy/component")
async def set_component_policy_handler(request):
json_data = await request.json()
set_component_policy(json_data['value'])
try:
set_component_policy(json_data['value'])
except (KeyError, TypeError):
return web.Response(status=400, text='Invalid request')
except ValueError as e:
return web.Response(status=400, text=str(e))
core.write_config()
return web.Response(status=200)

Expand All @@ -1838,7 +1863,12 @@ async def get_update_policy(request):
@routes.post("/manager/policy/update")
async def set_update_policy_handler(request):
json_data = await request.json()
set_update_policy(json_data['value'])
try:
set_update_policy(json_data['value'])
except (KeyError, TypeError):
return web.Response(status=400, text='Invalid request')
except ValueError as e:
return web.Response(status=400, text=str(e))
core.write_config()
return web.Response(status=200)

Expand Down
28 changes: 22 additions & 6 deletions js/comfyui-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ var switch_comfyui_button = null;
var update_all_button = null;
var restart_stop_button = null;
var update_policy_combo = null;
var update_policy_chosen = false;

let share_option = 'all';
const share_options = [
Expand Down Expand Up @@ -642,10 +643,12 @@ async function switchComfyUI() {

showVersionSelectorDialog(versions, obj.current, async (selected_version) => {
if(selected_version == 'nightly') {
update_policy_chosen = true;
update_policy_combo.value = 'nightly-comfyui';
api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: 'nightly-comfyui' }) });
}
else {
update_policy_chosen = true;
update_policy_combo.value = 'stable-comfyui';
api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: 'stable-comfyui' }) });
}
Expand Down Expand Up @@ -996,11 +999,14 @@ class ManagerMenuDialog extends ComfyDialog {
this.datasrc_combo.appendChild($el('option', { value: 'local', text: 'Local' }, []));
this.datasrc_combo.appendChild($el('option', { value: 'remote', text: 'Channel (remote)' }, []));

// a choice made before the stored value arrives is not to be replaced by it
let db_mode_chosen = false;
api.fetchApi('/manager/db_mode')
.then(response => response.text())
.then(data => { this.datasrc_combo.value = data; });
.then(data => { if(!db_mode_chosen) this.datasrc_combo.value = data; });

this.datasrc_combo.addEventListener('change', function (event) {
db_mode_chosen = true;
api.fetchApi('/manager/db_mode', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) });
});

Expand Down Expand Up @@ -1142,14 +1148,18 @@ class ManagerMenuDialog extends ComfyDialog {
share_combo.appendChild($el('option', { value: option[0], text: `${option[1]}` }, []));
}

let share_option_chosen = false;
api.fetchApi('/manager/share_option')
.then(response => response.text())
.then(data => {
share_option = to_share_option(data);
share_combo.value = share_option;
if(!share_option_chosen) {
share_option = to_share_option(data);
share_combo.value = share_option;
}
});

share_combo.addEventListener('change', function (event) {
share_option_chosen = true;
const value = event.target.value;
share_option = value;
api.fetchApi(`/manager/share_option?value=${value}`);
Expand All @@ -1169,14 +1179,18 @@ class ManagerMenuDialog extends ComfyDialog {
component_policy_combo.appendChild($el('option', { value: 'workflow', text: 'Use workflow version' }, []));
component_policy_combo.appendChild($el('option', { value: 'higher', text: 'Use higher version' }, []));
component_policy_combo.appendChild($el('option', { value: 'mine', text: 'Use my version' }, []));
let component_policy_chosen = false;
api.fetchApi('/manager/policy/component')
.then(response => response.text())
.then(data => {
component_policy_combo.value = data;
set_component_policy(data);
if(!component_policy_chosen) {
component_policy_combo.value = data;
set_component_policy(data);
}
});

component_policy_combo.addEventListener('change', function (event) {
component_policy_chosen = true;
api.fetchApi('/manager/policy/component', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) });
set_component_policy(event.target.value);
});
Expand All @@ -1192,10 +1206,12 @@ class ManagerMenuDialog extends ComfyDialog {
api.fetchApi('/manager/policy/update')
.then(response => response.text())
.then(data => {
update_policy_combo.value = data;
if(!update_policy_chosen)
update_policy_combo.value = data;
});

update_policy_combo.addEventListener('change', function (event) {
update_policy_chosen = true;
api.fetchApi('/manager/policy/update', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ value: event.target.value }) });
});

Expand Down
41 changes: 41 additions & 0 deletions tests/test_comfyui_versions_failure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""`comfyui_versions` answers 400 when the versions of ComfyUI cannot be read.
Each case runs the real handler of `glob/manager_server.py`; `core` is replaced.
"""
import ast
import asyncio
import logging
import sys
from types import SimpleNamespace

from aiohttp import web

from manager_test_utils import GLOB_DIR


def fail():
raise RuntimeError("not a git repository")


def answer(get_comfyui_versions):
"""The status and text `comfyui_versions` answers."""
source = (GLOB_DIR / "manager_server.py").read_text(encoding="utf-8")
handler = [node for node in ast.parse(source).body
if isinstance(node, ast.AsyncFunctionDef) and node.name == "comfyui_versions"]
assert len(handler) == 1
namespace = {"web": web, "logging": logging, "sys": sys,
"core": SimpleNamespace(get_comfyui_versions=get_comfyui_versions),
"routes": SimpleNamespace(get=lambda path: lambda handler: handler)}
exec(compile(ast.Module(body=handler, type_ignores=[]), "<comfyui_versions>", "exec"), namespace)
response = asyncio.run(namespace["comfyui_versions"](None))
return response.status, response.text


def test_version_list_that_cannot_be_read_is_answered_with_400(caplog):
assert answer(fail) == (400, None)
assert "ComfyUI update fail: not a git repository" in caplog.text


def test_version_list_is_answered_with_the_versions():
assert answer(lambda: (["nightly", "v0.3.1"], "v0.3.1", "v0.3.1")) == (
200, '{"versions": ["nightly", "v0.3.1"], "current": "v0.3.1"}')
Loading
Loading