dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
149 stars 42 forks source link

[native_assets_builder] Concurrent execution of `NativeAssetsBuilder` #1319

Closed dcharkes closed 2 months ago

dcharkes commented 3 months ago

Currently, NativeAssetsBuilders methods can be called by the embedder as follows:

This is currently mostly fine.

The situation in which this breaks in Flutter is when flutter run -d all has overlapping devices. E.g. if there are two Android phones connected. (It works fine with MacOS, iOS Simuatlor, iOS device, and Android emulator together.)

Flutter does not allow running more than one flutter command in the terminal concurrently. It has some kind of lock.

Dart doesn't have any commands running multiple native asset builds.

However, Dart doesn't guard against concurrent invocations. So two concurrent dart run process runs will cause undefined behavior.

Also there's no guard against running dart and flutter concurrently.

We should explore making the NativeAssetsBuilder re-entrant. It should support the following scenarios:

Since multiple processes need to communicate, we'd likely want to use lock files on the file system.

dart:io surfaces locks: https://api.dart.dev/stable/3.4.4/dart-io/RandomAccessFile/lock.html

dcharkes commented 3 months ago

Due to flutter run -d all running native assets builds concurrently, we cannot share the hook compilation to kernel across the same hook but different config invocations in https://github.com/dart-lang/native/issues/1256.