techfreakworm commited on
Commit
8e8d352
·
unverified ·
1 Parent(s): 2c7871d

fix(app): print the bootstrap summary last and exit 1 on any failure

Browse files

The Space API keeps only the last 2000 characters of output. A failed
bootstrap now prints its traceback first, then "[ams] model bootstrap
failed:" with the short per-root summary as the very last lines, then
raises SystemExit(1) so no second traceback follows. Any exception takes
this path, not only ModelStoreError, so a bug or an OS error while
linking checkpoints also ends with a readable last line.

Files changed (2) hide show
  1. app.py +10 -5
  2. tests/test_preload_repos.py +20 -10
app.py CHANGED
@@ -87,6 +87,7 @@ print(f"[ams] sys.path patched (vendor exists: {_VENDORED_ACE_STEP.exists()})",
87
  import hashlib
88
  import random
89
  import shutil # noqa: F401 (reserved for future cleanup paths)
 
90
 
91
  import gradio as gr
92
 
@@ -136,16 +137,20 @@ def _symlink_ace_step_checkpoints() -> None:
136
  def _bootstrap_spaces_cache() -> None:
137
  """On HF Spaces, make the pinned model snapshots available before the UI starts.
138
 
139
- No-op locally. A required repo that cannot be found or downloaded ends the
140
- import with the model store's short summary as the last lines of output:
141
- the Space API keeps only the last 2000 characters of the log.
 
142
  """
143
  if not os.getenv("SPACE_ID"):
144
  return
145
  try:
146
  _symlink_ace_step_checkpoints()
147
- except model_store.ModelStoreError as e:
148
- raise SystemExit(f"[ams] model bootstrap failed:\n{e}") from None
 
 
 
149
 
150
 
151
  def _warm_demucs_on_spaces() -> None:
 
87
  import hashlib
88
  import random
89
  import shutil # noqa: F401 (reserved for future cleanup paths)
90
+ import traceback
91
 
92
  import gradio as gr
93
 
 
137
  def _bootstrap_spaces_cache() -> None:
138
  """On HF Spaces, make the pinned model snapshots available before the UI starts.
139
 
140
+ No-op locally. Any failure (a required repo that cannot be found or
141
+ downloaded, or anything else) prints the traceback, then the short summary
142
+ as the very last lines, and exits with status 1 without a second
143
+ traceback: the Space API keeps only the last 2000 characters of the log.
144
  """
145
  if not os.getenv("SPACE_ID"):
146
  return
147
  try:
148
  _symlink_ace_step_checkpoints()
149
+ except Exception as e:
150
+ traceback.print_exc()
151
+ summary = str(e) if isinstance(e, model_store.ModelStoreError) else f"{type(e).__name__}: {e}"
152
+ print(f"[ams] model bootstrap failed:\n{summary}", file=_sys.stderr, flush=True)
153
+ raise SystemExit(1) from None
154
 
155
 
156
  def _warm_demucs_on_spaces() -> None:
tests/test_preload_repos.py CHANGED
@@ -106,21 +106,31 @@ def test_bootstrap_delegates_to_model_store_on_spaces(monkeypatch, fake_startup,
106
  assert [c[0] for c in fake_startup.calls] == [tmp_path / "vendor" / "ace-step" / "checkpoints"]
107
 
108
 
109
- def test_required_repo_failure_ends_import_with_summary_last(monkeypatch, fake_startup):
110
- summary = (
111
- "could not get o/xl@"
112
- + "a" * 40
113
- + " (1 files, 0.00 GB) into any cache root:\n /r: PermissionError: no"
114
- )
115
- fake_startup.error = model_store.ModelStoreError(summary)
 
 
 
 
 
116
  monkeypatch.setenv("SPACE_ID", "test/test")
117
 
118
  with pytest.raises(SystemExit) as exc:
119
  app._bootstrap_spaces_cache()
120
 
121
- message = str(exc.value.code)
122
- assert message.endswith(summary)
123
- assert exc.value.__suppress_context__ # no chained traceback before the summary
 
 
 
 
 
124
 
125
 
126
  # --- README front matter (read by HF Spaces at build time) ------------------------
 
106
  assert [c[0] for c in fake_startup.calls] == [tmp_path / "vendor" / "ace-step" / "checkpoints"]
107
 
108
 
109
+ @pytest.mark.parametrize("kind", ["model_store", "other"])
110
+ def test_bootstrap_failure_prints_traceback_then_summary_last_and_exits_1(
111
+ monkeypatch, fake_startup, capsys, kind
112
+ ):
113
+ if kind == "model_store":
114
+ summary = (
115
+ "could not get o/xl@" + "a" * 40 + " (1 files, 0.00 GB): no cache root is usable\n /r: denied"
116
+ )
117
+ fake_startup.error = model_store.ModelStoreError(summary)
118
+ else: # a bug or an OS error outside the resolver ends the same way
119
+ summary = "OSError: [Errno 30] Read-only file system: 'checkpoints'"
120
+ fake_startup.error = OSError(30, "Read-only file system", "checkpoints")
121
  monkeypatch.setenv("SPACE_ID", "test/test")
122
 
123
  with pytest.raises(SystemExit) as exc:
124
  app._bootstrap_spaces_cache()
125
 
126
+ assert exc.value.code == 1
127
+ err = capsys.readouterr().err
128
+ assert "Traceback (most recent call last)" in err
129
+ assert err.index("Traceback") < err.rindex("[ams] model bootstrap failed:")
130
+ # The Space API keeps the last 2000 characters: the summary must be the last thing printed.
131
+ assert err.rstrip().endswith(summary.splitlines()[-1])
132
+ assert err.rstrip()[-2000:].count("[ams] model bootstrap failed:") == 1
133
+ assert summary.splitlines()[0] in err.rstrip()[-2000:]
134
 
135
 
136
  # --- README front matter (read by HF Spaces at build time) ------------------------