leanprover / leanprover/lean4

Confusing crash on infinite loop in metaprogram

Open
#6,172 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug code-generator P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Description

For some infinite loops, executing them in the interpreter leads to a crash with the message libc++abi: terminating due to uncaught exception of type lean::exception: unreachable code.

Context

This happened to me when I was working on a fairly complicated metaprogram involving environment extensions. It took a long time to figure out that it was an infinite loop - I'd have expected a program that didn't terminate, or one that crashed with the explicit infinite loop error, rather than this message.

Steps to Reproduce

Enter the following code:

import Lean
open Lean Elab Command

elab "#oops" : command => do
  let mut n : Nat := 0
  repeat
    n := n + 1
  logInfo (toString n)

#oops

Expected behavior:

Either nontermination, a heartbeats error, a maximum recursion depth error, or the "deep recursion" crash from this program:

import Lean
open Lean Elab Command

elab "#oops" : command => do
  repeat
    pure ()

#oops
libc++abi: terminating due to uncaught exception of type lean::throwable: deep recursion was detected at 'interpreter' (potential solution: increase stack space in your system)
interpreter stacktrace:

Actual behavior:

The Lean language server crashes with libc++abi: terminating due to uncaught exception of type lean::exception: unreachable code. This makes it difficult to figure out what's actually wrong, even when looking at it with lldb.

Versions

"4.12.0" and "4.14.0-rc2" both exhibit this on macOS 14.6.1.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided #oops metaprogram and compare it with the deep-recursion example. Trace the interpreter and language-server handling of the infinite loop until the unreachable-code exception is identified; done means the example no longer crashes with that misleading message and reports an expected termination or resource error.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.