From c4e1a1b387015cfac0b93ad25dd85b388a198a18 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Fri, 25 Aug 2023 14:30:07 -0700 Subject: [PATCH] Fix locking to work with flutter and dart running simultaneously (#133350) --- bin/dart | 3 ++- bin/flutter | 3 ++- bin/internal/shared.sh | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/bin/dart b/bin/dart index 0c1f402819..9ef65c2c43 100755 --- a/bin/dart +++ b/bin/dart @@ -45,6 +45,7 @@ function follow_links() ( PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")" BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" +SHARED_NAME="$BIN_DIR/internal/shared.sh" OS="$(uname -s)" # If we're on Windows, invoke the batch script instead to get proper locking. @@ -53,6 +54,6 @@ if [[ $OS =~ MINGW.* || $OS =~ CYGWIN.* || $OS =~ MSYS.* ]]; then fi # To define `shared::execute()` function -source "$BIN_DIR/internal/shared.sh" +source "$SHARED_NAME" shared::execute "$@" diff --git a/bin/flutter b/bin/flutter index 9347b8dad6..21c11c7e7e 100755 --- a/bin/flutter +++ b/bin/flutter @@ -50,6 +50,7 @@ function follow_links() ( PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")" BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" +SHARED_NAME="$BIN_DIR/internal/shared.sh" OS="$(uname -s)" # If we're on Windows, invoke the batch script instead to get proper locking. @@ -58,6 +59,6 @@ if [[ $OS =~ MINGW.* || $OS =~ CYGWIN.* || $OS =~ MSYS.* ]]; then fi # To define `shared::execute()` function -source "$BIN_DIR/internal/shared.sh" +source "$SHARED_NAME" shared::execute "$@" diff --git a/bin/internal/shared.sh b/bin/internal/shared.sh index 3532c23114..75d9d3013e 100644 --- a/bin/internal/shared.sh +++ b/bin/internal/shared.sh @@ -229,7 +229,23 @@ function shared::execute() { exit 1 fi - upgrade_flutter 7< "$PROG_NAME" + # File descriptor 7 is prepared here so that we can use it with + # flock(1) in _lock() (see above). + # + # We use number 7 because it's a luckier number than 3; luck is + # important when making locks work reliably. Also because that way + # if anyone is redirecting other file descriptors there's less + # chance of a conflict. + # + # In any case, the file we redirect into this file descriptor is + # this very source file you are reading right now, because that's + # the only file we can truly guarantee exists, since we're running + # it. We don't use PROG_NAME because otherwise if you run `dart` and + # `flutter` simultaneously they'll end up using different lock files + # and will corrupt each others' downloads. + # + # SHARED_NAME itself is prepared by the caller script. + upgrade_flutter 7< "$SHARED_NAME" BIN_NAME="$(basename "$PROG_NAME")" case "$BIN_NAME" in