dart-lang / dart-docker

Docker images for the Dart programming language (https://dart.dev)
BSD 3-Clause "New" or "Revised" License
70 stars 15 forks source link

Seg fault when building on Apple Silicon for linux/amd64 target - related to Dart SDK carat syntax in pubspec #172

Closed pattobrien closed 1 year ago

pattobrien commented 1 year ago

See reproducible repo here: https://github.com/pattobrien/docker_dart_build_failure

Docker + Dart Build Failure

When building a Dart app for linux/amd64 on a MacOS (M1) machine, the build fails with a seg fault error (11). However, when changing the dart sdk version in pubspec.yaml to use hyphen syntax (rather than carat), the build succeeds.

# pubspec.yaml
name: docker_dart_build_failure

environment:
  sdk: ^3.0.0
  # sdk: ">=3.0.0 <4.0.0" # Uncomment this to fix the Docker build failures

I believe carat syntax has become the new default as of Dart 3 when running dart create, so its likely that the switch will cause more people to see the issue over time.

FWIW - I found several issues that mentioned similar seg faults with workarounds or insufficient reproducibility steps, so I dug into reproducing the issue. Hope this info helps out.

Requirements

A MacOS (M1) machine with docker installed.

❯ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.6, on macOS 13.5.2 22G91 darwin-arm64, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc1)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[!] Android Studio (not installed)
[✓] VS Code (version 1.84.0)
[✓] VS Code (version 1.78.2)
[✓] Connected device (1 available)
[✓] Network resources

! Doctor found issues in 2 categories.

Steps to reproduce

Requirements: MacOS (M1)

  1. Clone the reproducible repo (or run dart create docker_dart_build_failure, which defaults to using carat syntax for the dart sdk env variable).
  2. Run docker build --platform=linux/amd64 . to run the docker build command.

Observed Error

Seg fault error (11) when compiling the dart app.

❯ sh build.sh
[+] Building 5.8s (10/12) docker:desktop-linux
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 548B 0.0s
=> [internal] load metadata for docker.io/library/dart:stable 0.4s
=> [auth] library/dart:pull token for registry-1.docker.io 0.0s
=> [build 1/5] FROM docker.io/library/dart:stable@sha256:2432c3e2162542ee9d701b9d8524a3220f0182d37407382efa8fd782d1273e82 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 21.39kB 0.0s
=> CACHED [build 2/5] WORKDIR /app 0.0s
=> [build 3/5] COPY . . 0.0s
=> [build 4/5] RUN dart pub get 4.1s
=> ERROR [build 5/5] RUN dart compile exe bin/docker_dart_build_failure.dart -o bin/docker_dart_build_failure 1.2s

> [build 5/5] RUN dart compile exe bin/docker_dart_build_failure.dart -o bin/docker_dart_build_failure:
> 1.149
> 1.149 ===== CRASH =====
> 1.149 si_signo=Trace/breakpoint trap(5), si_code=1, si_addr=0xffff7e421d39
> 1.149 version=3.1.5 (stable) (Tue Oct 24 04:57:17 2023 +0000) on "linux_x64"
> 1.149 pid=15, thread=20, isolate_group=main(0x5555578ce540), isolate=main(0x5555578d0da0)
> 1.149 os=linux, arch=x64, comp=no, sim=no
> 1.149 isolate_instructions=ffff7e69f000, vm_instructions=55555739dd20
> 1.149 fp=7ffff5cfaa30, sp=7ffff5cfa9f8, pc=ffff7e421d39
...
> 1.149 pc 0x00005555574db7f8 fp 0x00007ffff5cfd6d0 dart::DartEntry::InvokeFunction+0xc8
> 1.149 pc 0x00005555574dd376 fp 0x00007ffff5cfd710 dart::DartLibraryCalls::HandleMessage+0x126
> 1.149 pc 0x00005555574f8b7f fp 0x00007ffff5cfdca0 dart::IsolateMessageHandler::HandleMessage+0x2bf
> 1.149 pc 0x000055555751a657 fp 0x00007ffff5cfdd10 dart::MessageHandler::HandleMessages+0x127
> 1.149 pc 0x000055555751ac44 fp 0x00007ffff5cfdd60 dart::MessageHandler::TaskCallback+0x1e4
> 1.149 pc 0x0000555557617ccb fp 0x00007ffff5cfdde0 dart::ThreadPool::WorkerLoop+0x14b
> 1.149 pc 0x0000555557617f68 fp 0x00007ffff5cfde10 dart::ThreadPool::Worker::Main+0x78
> 1.149 pc 0x00005555575a19a6 fp 0x00007ffff5cfded0 dart::ThreadStart+0xd6
> 1.149 -- End of DumpStackTrace
> 1.156 Error: AOT compilation failed

## 1.157 Generating AOT kernel dill failed!

## Dockerfile:8

6 | RUN dart pub get
7 |
8 | >>> RUN dart compile exe bin/docker_dart_build_failure.dart -o bin/docker_dart_build_failure
9 |
10 | # Build minimal serving image from AOT-compiled `/server` and required system

---

ERROR: failed to solve: process "/bin/sh -c dart compile exe bin/docker_dart_build_failure.dart -o bin/docker_dart_build_failure" did not complete successfully: exit code: 64
mraleph commented 1 year ago

As I have already indicated in https://github.com/dart-lang/sdk/issues/48420 we are not going to be debugging this ourselves because there is a strong indication that its a bug in emulation layer - so there is very little we can do about it.

/cc @athomas could you close this?

kerberjg commented 10 months ago

The bug was apparently fixed in Docker Desktop 4.26.0 for Mac, but it seems to persist in Dart.

https://www.docker.com/blog/docker-desktop-4-26/

Building package executables... (1.4s)
===== CRASH =====
si_signo=Segmentation fault(11), si_code=1, si_addr=0x7fff4050adb0
version=3.1.3 (stable) (Tue Sep 26 14:25:13 2023 +0000) on "linux_x64"
pid=245, thread=253, isolate_group=main(0x5555578ce570), isolate=main(0x5555578d0dd0)
os=linux, arch=x64, comp=no, sim=no
isolate_instructions=ffff950b4000, vm_instructions=55555739dd20
fp=7ffff59fd680, sp=7ffff59fd630, pc=5555574ebd19
  pc 0x00005555574ebd19 fp 0x00007ffff59fd680 dart::DeoptInfo::UnpackInto+0x139
  pc 0x00005555574e9d71 fp 0x00007ffff59fd710 dart::DeoptContext::FillDestFrame+0x71
  pc 0x00005555575ea813 fp 0x00007ffff59fdcd0 DLRT_DeoptimizeFillFrame+0xb3
  pc 0x0000ffff94f83398 fp 0x00007ffff59fdce8 Unknown symbol
  pc 0x00007ffff7790a01 fp 0x00007ffff59fdd00 Unknown symbol
-- End of DumpStackTrace
mraleph commented 10 months ago

@kerberjg

The bug was apparently fixed in Docker Desktop 4.26.0 for Mac, but it seems to persist in Dart.

Can you check if you have "Use Virtualization framework" and "Use Rosetta for x86/AMD64 emulation on Apple Silicon" enabled in "General" tab of Docker settings?

kerberjg commented 10 months ago

I confirm they’re both enabled

mraleph commented 10 months ago

Thanks @kerberjg. It's a bummer it does not work. See https://github.com/dart-lang/sdk/issues/54446#issuecomment-1875995351 for the current state.

mraleph commented 10 months ago

Thanks to @parlough experimenting with some things we now have an idea of what was causing this (see comment) and are going to make a change to the VM which should work around this problem.

Unfortunately we have missed the release cutoff for 3.3, but I am planning to request cherry pick later.