From 9c588f32b846dd84f0c24d0980c4748419c5ee8c Mon Sep 17 00:00:00 2001 From: jensjoha Date: Tue, 20 Sep 2022 08:05:18 +0200 Subject: [PATCH] Fix flutter tool crash on upgrade caused by app-jit (#111879) Fix flutter tool crash on upgrade caused by app-jit --- bin/internal/shared.bat | 19 +++++++++++++++++++ bin/internal/shared.sh | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/bin/internal/shared.bat b/bin/internal/shared.bat index 9a3ee32b8db..26fea942199 100644 --- a/bin/internal/shared.bat +++ b/bin/internal/shared.bat @@ -16,6 +16,7 @@ SETLOCAL SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools SET cache_dir=%FLUTTER_ROOT%\bin\cache SET snapshot_path=%cache_dir%\flutter_tools.snapshot +SET snapshot_path_old=%cache_dir%\flutter_tools.snapshot.old SET stamp_path=%cache_dir%\flutter_tools.stamp SET script_path=%flutter_tools_dir%\bin\flutter_tools.dart SET dart_sdk_path=%cache_dir%\dart-sdk @@ -170,6 +171,21 @@ GOTO :after_subroutine POPD + REM Move the old snapshot - we can't just overwrite it as the VM might currently have it + REM memory mapped (e.g. on flutter upgrade), and deleting it might not work if the file + REM is in use. For downloading a new dart sdk the folder is moved, so we take the same + REM approach of moving the file here. + SET /A snapshot_path_suffix=1 + :move_old_snapshot + IF EXIST "%snapshot_path_old%%snapshot_path_suffix%" ( + SET /A snapshot_path_suffix+=1 + GOTO move_old_snapshot + ) ELSE ( + IF EXIST "%snapshot_path%" ( + MOVE "%snapshot_path%" "%snapshot_path_old%%snapshot_path_suffix%" 2> NUL > NUL + ) + ) + IF "%FLUTTER_TOOL_ARGS%" == "" ( "%dart%" --verbosity=error --snapshot="%snapshot_path%" --snapshot-kind="app-jit" --packages="%flutter_tools_dir%\.dart_tool\package_config.json" --no-enable-mirrors "%script_path%" > NUL ) else ( @@ -182,6 +198,9 @@ GOTO :after_subroutine ) >"%stamp_path%" ECHO %compilekey% + REM Try to delete any old snapshots now. Swallow any errors though. + DEL "%snapshot_path%.old*" 2> NUL > NUL + REM Exit Subroutine EXIT /B diff --git a/bin/internal/shared.sh b/bin/internal/shared.sh index 10ab14934bb..76717114f54 100644 --- a/bin/internal/shared.sh +++ b/bin/internal/shared.sh @@ -154,9 +154,22 @@ function upgrade_flutter () ( fi pub_upgrade_with_retry + # Move the old snapshot - we can't just overwrite it as the VM might currently have it + # memory mapped (e.g. on flutter upgrade). For downloading a new dart sdk the folder is moved, + # so we take the same approach of moving the file here. + SNAPSHOT_PATH_OLD="$SNAPSHOT_PATH.old" + if [ -f "$SNAPSHOT_PATH" ]; then + mv "$SNAPSHOT_PATH" "$SNAPSHOT_PATH_OLD" + fi + # Compile... "$DART" --verbosity=error --disable-dart-dev $FLUTTER_TOOL_ARGS --snapshot="$SNAPSHOT_PATH" --snapshot-kind="app-jit" --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" --no-enable-mirrors "$SCRIPT_PATH" > /dev/null echo "$compilekey" > "$STAMP_PATH" + + # Delete any temporary snapshot path. + if [ -f "$SNAPSHOT_PATH_OLD" ]; then + rm -f "$SNAPSHOT_PATH_OLD" + fi fi # The exit here is extraneous since the function is run in a subshell, but # this serves as documentation that running the function in a subshell is