Skip to content

[CuTe DSL] Fix REPL crash: clear diagnostic and keep the interpreter alive (issue #3413) - #3424

Open
layahaasini wants to merge 2 commits into
NVIDIA:mainfrom
layahaasini:fix/cutedsl-repl-crash-3413
Open

[CuTe DSL] Fix REPL crash: clear diagnostic and keep the interpreter alive (issue #3413)#3424
layahaasini wants to merge 2 commits into
NVIDIA:mainfrom
layahaasini:fix/cutedsl-repl-crash-3413

Conversation

@layahaasini

@layahaasini layahaasini commented Jul 30, 2026

Copy link
Copy Markdown

Fixes #3413

Calling a @cute.jit function defined in the REPL (or via exec()) causes 2 problems:

  1. The "no source available" failure gets raised as DSLRuntimeError, rendering as "[Internal Error] This is a bug in the DSL ... please report this". It isn't a DSL bug, REPL is a known unsupported case ([BUG] Cutlass Python DSL: could not get source code #2636 says a clear message was added for it in 4.3, but it doesn't reach the user because of the error class).
  2. The DSL excepthook unconditionally calls sys.exit(1), so the uncaught error kills the whole interactive session instead of returning to the prompt. Same thing happens when debugging a normal file-based kernel with python -i.

Changes:

  • ast_preprocessor.py: catch OSError from inspect.getsourcelines separately and raise DSLUserCodeError with a new catalog entry UNSUP_NO_SOURCE ("save the function to a .py file"). Other parse failures still raise DSLRuntimeError, now with the cause chained.
  • common.py: skip the sys.exit(1) in _dsl_excepthook when the session is interactive. Scripts still exit 1 on uncaught DSL errors.

This doesn't add REPL support, it just makes the failure behave as intended.

With the patch, the repro from the issue now gives:

>>> run(lambda: 0)
error[UNSUP_NO_SOURCE]: The source of `run` is not available (e.g. defined in the REPL or via exec()), so it cannot be compiled.
  = note: Caused exception: could not get source code
  suggestion: Save the function to a .py file and import it from there.
>>> print("still alive")
still alive

Tested against the 4.6.1 wheel on a DGX Spark (GB10): the two new regression tests in test/python/CuTeDSL/test_no_source_diagnostics.py fail on the stock wheel and pass with the patch; a third checks script exit status is unchanged. Also verified a normal file-based kernel still compiles and launches.

Note: #3396 touches the same except block for a different bug (stale on-disk source), small rebase likely needed

@layahaasini
layahaasini force-pushed the fix/cutedsl-repl-crash-3413 branch from 2cbcd71 to 9094546 Compare July 30, 2026 21:28
A @cute.jit function defined in the REPL (or via exec()) has no
retrievable source, which raised an "[Internal Error] this is a bug in
the DSL" envelope and then killed the whole interactive session via the
DSL excepthook's unconditional sys.exit(1).

- Raise a user-facing DSLUserCodeError (new DiagId.UNSUP_NO_SOURCE) for
  the no-source case, keeping DSLRuntimeError for real parse failures
- Skip sys.exit(1) in the excepthook for interactive sessions; script
  exit status is unchanged

Fixes NVIDIA#3413
@layahaasini
layahaasini force-pushed the fix/cutedsl-repl-crash-3413 branch from 9094546 to 575cf49 Compare July 30, 2026 21:36
@layahaasini
layahaasini marked this pull request as ready for review July 30, 2026 21:40
@anakinxc

anakinxc commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

LGTM, thanks for contributing

@brandon-yujie-sun

@brandon-yujie-sun brandon-yujie-sun left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a couple comments, and thanks for contributing the changes!

print(str(exc_value), file=sys.stderr)
sys.exit(1)
# Don't kill an interactive session (REPL, `python -i`)
if not (hasattr(sys, "ps1") or sys.flags.interactive):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides -i, can we also cover the PYTHONINSPECT=1 case which sets sys.flags.inspect?

f"Failed to parse function {func_name}",
suggestion="DSL does not support REPL mode, save the function to a file instead.",
)
except OSError as e:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems OSError might not be enough: https://docs.python.org/3/library/inspect.html#inspect.getsourcelines Can we also handle the TypeError?

@layahaasini

Copy link
Copy Markdown
Author

Done, thanks for the feedback!

@brandon-yujie-sun

Copy link
Copy Markdown
Collaborator

@Junkai-Wu for further proceeding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Using CuTe DSL in REPL crashes Python

4 participants