modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.06k stars 2.59k forks source link

[BUG] Segmentation Fault in VSCode Terminal but not in iTerm on MacOS #3010

Open kseuro opened 4 months ago

kseuro commented 4 months ago

Bug description

Running a mojo script using VSCode's integrated terminal produces a segmentation fault but that same script runs successfully if executed using either iTerm or the Mac Terminal application.

Stack Trace

Executing the same script (detailed below) ends with two different outcomes.

VSCode Terminal

Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo merge_sort/merge_sort.mojo
 #0 0x0000000105098c04 llvm_strlcpy (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x1000c0c04)
 #1 0x0000000105096ef0 llvm_strlcpy (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x1000beef0)
 #2 0x00000001050992a4 llvm_strlcpy (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x1000c12a4)
 #3 0x00000001a6b3ea24 (/usr/lib/system/libsystem_platform.dylib+0x18042ea24)
 #4 0x000000028011c5b0
 #5 0x00000002800c0050
 #6 0x000000010542d510 __jit_debug_register_code (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x100455510)
 #7 0x0000000104ff954c _mh_execute_header (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x10002154c)
 #8 0x0000000104ff8f40 _mh_execute_header (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x100020f40)
 #9 0x0000000104fe1940 _mh_execute_header (/Users/kaistewart/.modular/pkg/packages.modular.com_max/bin/mojo+0x100009940)
#10 0x00000001a67b7f28
mojo crashed!
Please file a bug report.
[72902:9559005:20240611,084425.631377:WARNING in_range_cast.h:38] value -634136515 out of range
[72902:9559005:20240611,084425.637020:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
Segmentation fault: 11

iTerm or Terminal App

Sorted:       1000 elements in   0.00007 Seconds

Steps to reproduce

Minimal Code Example

import benchmark
from python import Python
from random import random_si64

fn merge_sort(inout unsorted_array: List[Int]):
    if len(unsorted_array) > 1:
        var middle = int(len(unsorted_array) / 2)
        var left = unsorted_array[:middle]
        var right = unsorted_array[middle:]

        merge_sort(left)
        merge_sort(right)

        var i = 0
        var j = 0
        var k = 0

        while i < len(left) and j < len(right):
            if left[i] < right[j]:
                unsorted_array[k] = left[i]
                i += 1
            else:
                unsorted_array[k] = right[j]
                j += 1
            k += 1

        while i < len(left):
            unsorted_array[k] = left[i]
            i += 1
            k += 1

        while j < len(right):
            unsorted_array[k] = right[j]
            j += 1
            k += 1

@always_inline
fn bench[func: fn (inout List[Int]) -> None, size: Int]() raises:
    """Benchmarking function."""

    var target_array = List[Int]()
    for _ in range(size):
        target_array.append(int(random_si64(0, size)))

    @always_inline
    @parameter
    fn test_fn():
        func(target_array)

    var seconds = benchmark.run[test_fn](max_runtime_secs=10).mean()

    var py = Python.import_module("builtins")
    _ = py.print(
        py.str("{:<13} {:>2} elements in {:>9.5f} Seconds").format(
            "Sorted:", len(target_array), seconds
        )
    )

fn main() raises -> None:
    """Driver function."""
    bench[merge_sort, 1000]()

System information

- What OS did you do install Mojo on ?
> MacOS Ventura 13.6.6

- Provide version information for Mojo by pasting the output of `mojo -v`
> mojo 24.4.0 (59977802)

- Provide Modular CLI version by pasting the output of `modular -v`
> modular 0.8.0 (39a426b5)
walter-erquinigo commented 3 months ago

Thanks for reporting this. The only thing I can think of is the python configuration. Can you make sure that the python related environment variables are the same on your vscode terminal and on your Iterm2? I know that VSCode sometimes sets some python environment automatically.

kseuro commented 3 months ago

Thanks for the reply.

When I enter which python in iTerm or VSCode, after activating the max conda environment I installed via this guide, I get miniconda3/envs/max/bin/python.

It's a bit confusing because I can run other .mojo scripts using VSCode's built-in terminal; for instance, this reproduction of the MatMul example consistently works in iTerm and VSCode. The above posted merge sort will occasionally crash in VSCode but always succeed in iTerm. So how can it be that the scripts are sometimes executed successfully and sometimes encounter a segfault? I'm no expert but this feels like a system level problem.

walter-erquinigo commented 3 months ago

Could you run env on both terminals and share the output here? Feel free to erase sensitive information. This might help us get to the core of the issue

kseuro commented 2 months ago

Sure, here is the output of env. I've replaced my user path with the placeholder ...

TERM_PROGRAM=tmux
TERM=screen-256color
SHELL=/bin/bash
TMPDIR=/var/folders/y_/r_vf3lkx5h1_2ldljw8z99xm0000gn/T/
CONDA_SHLVL=8
TERM_PROGRAM_VERSION=3.4
CONDA_PROMPT_MODIFIER=(max)
OLDPWD=../projects/modular/max
TERM_SESSION_ID=w0t0p0:5A443A7B-A3FB-4F7B-BEF1-BA2501BD110F
USER=..
COMMAND_MODE=unix2003
CONDA_EXE=../miniconda3/bin/conda
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.gNk9uvBdP4/Listeners
__CF_USER_TEXT_ENCODING=0x0:0:0
CONDA_PREFIX_5=../miniconda3
TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF
CONDA_PREFIX_4=../miniconda3/envs/max
CONDA_PREFIX_7=../miniconda3
TMUX=/private/tmp/tmux-501/default,14905,0
CONDA_PREFIX_6=../miniconda3/envs/max
_CE_CONDA=
CONDA_PREFIX_1=../miniconda3
TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo
PATH=../.modular/pkg/packages.modular.com_max/bin:../.modular/pkg/packages.modular.com_max/bin:../.modular/pkg/packages.modular.com_max/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/opt/homebrew/opt/llvm/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:../.modular/pkg/packages.modular.com_max/bin:../miniconda3/envs/max/bin:../miniconda3/condabin:/opt/homebrew/sbin:/opt/homebrew/bin:/opt/homebrew/opt/llvm/bin:/Applications/iTerm.app/Contents/Resources/utilities
CONDA_PREFIX_3=../miniconda3
CONDA_PREFIX_2=../miniconda3/envs/max
LaunchInstanceID=01BD2081-B110-4464-A91E-9D62746AECF3
__CFBundleIdentifier=com.googlecode.iterm2
CONDA_PREFIX=../miniconda3/envs/max
PWD=../projects/modular/max/examples/inference/bert-python-torchscript
LANG=en_US.UTF-8
XPC_FLAGS=0x0
TMUX_PANE=%0
_CE_M=
XPC_SERVICE_NAME=0
SHLVL=2
HOME=..
COLORFGBG=15;0
LC_TERMINAL_VERSION=3.5.3
ITERM_SESSION_ID=w0t0p0:5A443A7B-A3FB-4F7B-BEF1-BA2501BD110F
LOGNAME=..
CONDA_PYTHON_EXE=../miniconda3/bin/python
MODULAR_HOME=../.modular
CONDA_DEFAULT_ENV=max
LC_TERMINAL=iTerm2
SECURITYSESSIONID=186ac
COLORTERM=truecolor
_=/usr/bin/env
walter-erquinigo commented 2 months ago

Cool. Could you also share the env of the other terminal? I'm looking for discrepancies that can explain the issue.