Skip to content

Commit aebd23e

Browse files
authored
Fix repo deletion test flake (flutter#195349)
Some tests have flaked because of a failed recursive directory deletion, for example in [this run's output](https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8668009021583785713/+/u/run_test.dart_for_framework_tests_shard_and_subshard_misc/stdout): ``` 00:02 +69 ~2 -1: /b/s/w/ir/x/w/flutter/dev/tools/test/last_engine_commit_test.dart: if there have been no engine changes, uses the first commit since the branch point [E] FileSystemException: Deletion failed, path = '/b/s/w/ir/x/t/last_engine_commit_test.SDNXWC' (OS Error: Directory not empty, errno = 39) dart:io FileSystemEntity.deleteSync package:file/src/forwarding/forwarding_file_system_entity.dart 70:16 ForwardingFileSystemEntity.deleteSync test/last_engine_commit_test.dart 124:12 main.<fn> ``` A likely cause is that git is making changes to the filesystem in the background, so I've disabled detached mode for git. I also made it retry the deletion if it fails. I wasn't able to repro locally, so I won't close the issue until I can confirm that the flake doesn't happen again. Related to: flutter#194368
1 parent 3b3facf commit aebd23e

4 files changed

Lines changed: 72 additions & 19 deletions

File tree

‎dev/tools/test/content_aware_hash_test.dart‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:file/local.dart';
1212
import 'package:platform/platform.dart';
1313
import 'package:test/test.dart';
1414

15+
import 'temp_git_repo_utils.dart';
16+
1517
//////////////////////////////////////////////////////////////////////
1618
// //
1719
// ✨ THINKING OF MOVING/REFACTORING THIS FILE? READ ME FIRST! ✨ //
@@ -81,7 +83,7 @@ void main() {
8183
tmpDir = localFs.systemTempDirectory.createTempSync('content_aware_hash.');
8284
testRoot = _FlutterRootUnderTest.fromPath(tmpDir.childDirectory('flutter').path);
8385

84-
environment = <String, String>{};
86+
environment = <String, String>{...kDisableGitAutoMaintenanceEnvironment};
8587

8688
if (const LocalPlatform().isWindows || usePowershellOnPosix) {
8789
// Copy a minimal set of environment variables needed to run the update_engine_version script in PowerShell.
@@ -104,15 +106,12 @@ void main() {
104106
..deps.createSync(recursive: true);
105107
});
106108

107-
tearDown(() {
109+
tearDown(() async {
108110
// Git adds a lot of files, we don't want to test for them.
109-
final Directory gitDir = testRoot.root.childDirectory('.git');
110-
if (gitDir.existsSync()) {
111-
gitDir.deleteSync(recursive: true);
112-
}
111+
await deleteTempDirectory(testRoot.root.childDirectory('.git'));
113112

114113
// Now do cleanup so even if the next step fails, we still deleted tmp.
115-
tmpDir.deleteSync(recursive: true);
114+
await deleteTempDirectory(tmpDir);
116115
});
117116

118117
/// Runs `bin/internal/content_aware_hash.{sh|ps1}` and returns the process result.

‎dev/tools/test/last_engine_commit_test.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:file/local.dart';
1212
import 'package:platform/platform.dart';
1313
import 'package:test/test.dart';
1414

15+
import 'temp_git_repo_utils.dart';
16+
1517
//////////////////////////////////////////////////////////////////////
1618
// //
1719
// ✨ THINKING OF MOVING/REFACTORING THIS FILE? READ ME FIRST! ✨ //
@@ -99,7 +101,7 @@ void main() {
99101
forcePowershell: usePowershellOnPosix,
100102
);
101103

102-
environment = <String, String>{};
104+
environment = <String, String>{...kDisableGitAutoMaintenanceEnvironment};
103105

104106
if (const LocalPlatform().isWindows || usePowershellOnPosix) {
105107
// Copy a minimal set of environment variables needed to run the update_engine_version script in PowerShell.
@@ -120,8 +122,8 @@ void main() {
120122
initGitRepoWithBlankInitialCommit();
121123
});
122124

123-
tearDown(() {
124-
tmpDir.deleteSync(recursive: true);
125+
tearDown(() async {
126+
await deleteTempDirectory(tmpDir);
125127
});
126128

127129
/// Runs `bin/internal/last_engine_commit.{sh|ps1}` and returns the stdout.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:file/file.dart';
6+
7+
/// Environment variables that turn off Git's automatic maintenance and garbage
8+
/// collection for every `git` process that receives them.
9+
///
10+
/// Commands like `git commit` and `git fetch` can start
11+
/// `git maintenance run --auto` as a detached background process (the default
12+
/// in recent versions of Git). That process may still be writing to `.git`
13+
/// after the command returns, so a test that deletes its temporary repository
14+
/// right away can fail with "Directory not empty".
15+
/// See https://github.com/flutter/flutter/issues/194368.
16+
///
17+
/// Passing these through the environment, rather than with `git config`,
18+
/// also covers repositories and `git` commands created by the scripts under
19+
/// test. Requires Git 2.31 or newer.
20+
const Map<String, String> kDisableGitAutoMaintenanceEnvironment = <String, String>{
21+
'GIT_CONFIG_COUNT': '3',
22+
'GIT_CONFIG_KEY_0': 'maintenance.auto',
23+
'GIT_CONFIG_VALUE_0': 'false',
24+
'GIT_CONFIG_KEY_1': 'gc.auto',
25+
'GIT_CONFIG_VALUE_1': '0',
26+
'GIT_CONFIG_KEY_2': 'gc.autoDetach',
27+
'GIT_CONFIG_VALUE_2': 'false',
28+
};
29+
30+
/// Recursively deletes [directory], retrying briefly if the deletion fails.
31+
///
32+
/// Deleting a temporary Git repository can fail transiently if another process
33+
/// is still writing to it, or on Windows if a file handle has not been released
34+
/// yet. After the last attempt, the failure is printed instead of thrown,
35+
/// because failing to clean up a temporary directory is not a test failure.
36+
///
37+
/// Returns whether [directory] was deleted.
38+
Future<bool> deleteTempDirectory(Directory directory, {int attempts = 5}) async {
39+
for (var attempt = 1; ; attempt++) {
40+
try {
41+
if (directory.existsSync()) {
42+
directory.deleteSync(recursive: true);
43+
}
44+
return true;
45+
} on FileSystemException catch (error) {
46+
if (attempt >= attempts) {
47+
print('Failed to delete ${directory.path}: $error');
48+
return false;
49+
}
50+
await Future<void>.delayed(Duration(milliseconds: 100 * attempt));
51+
}
52+
}
53+
}

‎dev/tools/test/update_engine_version_test.dart‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:file/local.dart';
1212
import 'package:platform/platform.dart';
1313
import 'package:test/test.dart';
1414

15+
import 'temp_git_repo_utils.dart';
16+
1517
//////////////////////////////////////////////////////////////////////
1618
// //
1719
// ✨ THINKING OF MOVING/REFACTORING THIS FILE? READ ME FIRST! ✨ //
@@ -85,7 +87,7 @@ void main() {
8587
forcePowershell: usePowershellOnPosix,
8688
);
8789

88-
environment = <String, String>{};
90+
environment = <String, String>{...kDisableGitAutoMaintenanceEnvironment};
8991

9092
if (const LocalPlatform().isWindows || usePowershellOnPosix) {
9193
// Copy a minimal set of environment variables needed to run the update_engine_version script in PowerShell.
@@ -116,12 +118,9 @@ void main() {
116118
}
117119
});
118120

119-
tearDown(() {
121+
tearDown(() async {
120122
// Git adds a lot of files, we don't want to test for them.
121-
final Directory gitDir = testRoot.root.childDirectory('.git');
122-
if (gitDir.existsSync()) {
123-
gitDir.deleteSync(recursive: true);
124-
}
123+
await deleteTempDirectory(testRoot.root.childDirectory('.git'));
125124

126125
// Take a snapshot of files we expect to be created or otherwise exist.
127126
//
@@ -156,7 +155,7 @@ void main() {
156155
);
157156

158157
// Now do cleanup so even if the next step fails, we still deleted tmp.
159-
tmpDir.deleteSync(recursive: true);
158+
await deleteTempDirectory(tmpDir);
160159

161160
final Set<String> unexpectedFiles = currentFiles.difference(expectedFiles);
162161
if (unexpectedFiles.isNotEmpty) {
@@ -286,8 +285,8 @@ void main() {
286285
expect(engineStamp, isNot(equals(externalHead)));
287286
});
288287

289-
tearDown(() {
290-
externalGit.deleteSync(recursive: true);
288+
tearDown(() async {
289+
await deleteTempDirectory(externalGit);
291290
});
292291
});
293292

0 commit comments

Comments
 (0)