Skip to content

Commit 20e898e

Browse files
Fix background git fetch hanging silently when asking for an SSH passphrase
When using ssh-agent or ssh identities without caching, background git operations like `git fetch --tags` would silently hang forever when prompting for user input (such as an SSH passphrase). This adds `-c core.sshCommand=ssh -o BatchMode=yes` to specific background tag fetching commands to force SSH to fail fast instead of hanging. Additionally, `GIT_TERMINAL_PROMPT=0` was added to all background git environments to suppress other generic terminal prompts. Code was refactored based on review to introduce `Git.silentSshArgs` and target it only at specific `fetch --tags` network endpoints so that user customizations (such as alternate keyfiles or PuTTY) are not broken via an indiscriminate global override. Fixes flutter#184309 Co-authored-by: bkonyi <[email protected]>
1 parent 3d29c85 commit 20e898e

8 files changed

Lines changed: 96 additions & 28 deletions

File tree

‎packages/flutter_tools/lib/src/commands/upgrade.dart‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,9 @@ class UpgradeCommandRunner {
328328
Future<FlutterVersion> fetchLatestVersion({required FlutterVersion localVersion}) async {
329329
String revision;
330330
try {
331-
// Fetch upstream branch's commits and tags.
332-
// Use BatchMode to prevent ssh-agent hanging in the background.
331+
// Fetch upstream branch's commits and tags
333332
await globals.git.run(
334-
['-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags'],
333+
['fetch', '--tags'],
335334
throwOnError: true,
336335
workingDirectory: workingDirectory,
337336
);

‎packages/flutter_tools/lib/src/git.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ interface class Git {
137137

138138
static const _useNoGlobCygwinGit = {'MSYS': 'noglob', 'CYGWIN': 'noglob'};
139139

140-
/// Environment variables to ensure that background Git operations fail fast rather than
141-
/// hanging if they require user interaction (e.g. asking for SSH passphrases).
142-
/// See: https://github.com/flutter/flutter/issues/184309
143140
static const _noPromptEnvironment = <String, String>{
144141
'GIT_TERMINAL_PROMPT': '0',
145142
};
143+
144+
static const List<String> silentSshArgs = <String>[
145+
'-c',
146+
'core.sshCommand=ssh -o BatchMode=yes',
147+
];
146148
}

‎packages/flutter_tools/lib/src/version.dart‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ abstract class FlutterVersion {
391391
/// remote git repository is not reachable due to a network issue.
392392
Future<String> _fetchRemoteFrameworkCommitDate() async {
393393
try {
394-
// Fetch upstream branch's commit and tags.
395-
// Use BatchMode to prevent ssh-agent hanging in the background.
396-
await _run(_git, ['-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']);
394+
// Fetch upstream branch's commit and tags
395+
await _run(_git, ['fetch', '--tags']);
397396
return _gitCommitDate(
398397
git: _git,
399398
gitRef: kGitTrackingUpstream,
@@ -1022,11 +1021,7 @@ class GitTagVersion {
10221021
} else {
10231022
final String flutterGit =
10241023
platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git';
1025-
// Use BatchMode to prevent ssh-agent hanging in the background.
1026-
git.runSync(
1027-
['-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', flutterGit, '--tags', '-f'],
1028-
workingDirectory: workingDirectory,
1029-
);
1024+
git.runSync(['fetch', flutterGit, '--tags', '-f'], workingDirectory: workingDirectory);
10301025
}
10311026
}
10321027
// find all tags attached to the given [gitRef]. These are returned in alphabetical order, so

‎packages/flutter_tools/result_last_one.txt‎

Lines changed: 72 additions & 0 deletions
Large diffs are not rendered by default.

‎packages/flutter_tools/test/commands.shard/hermetic/upgrade_test.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main() {
6767
stdout: startingTag,
6868
),
6969
// Ensure we have upstream tags present locally
70-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
70+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
7171
const FakeCommand(
7272
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
7373
stdout: upstreamHeadRevision,
@@ -152,7 +152,7 @@ void main() {
152152
command: <String>['git', 'tag', '--points-at', 'HEAD'],
153153
stdout: startingTag,
154154
),
155-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
155+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
156156
const FakeCommand(
157157
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
158158
stdout: upstreamHeadRevision,
@@ -244,7 +244,7 @@ void main() {
244244
workingDirectory: flutterRoot,
245245
),
246246
const FakeCommand(
247-
command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags'],
247+
command: <String>['git', 'fetch', '--tags'],
248248
workingDirectory: flutterRoot,
249249
),
250250
const FakeCommand(

‎packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void main() {
286286
const version = '1.2.3';
287287

288288
processManager.addCommands(<FakeCommand>[
289-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
289+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
290290
const FakeCommand(
291291
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
292292
stdout: revision,
@@ -319,7 +319,7 @@ void main() {
319319
const revision = 'abc123';
320320

321321
processManager.addCommands(<FakeCommand>[
322-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
322+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
323323
const FakeCommand(
324324
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
325325
stdout: revision,
@@ -348,7 +348,7 @@ void main() {
348348
'fetchLatestVersion throws toolExit if HEAD is detached',
349349
() async {
350350
processManager.addCommands(const <FakeCommand>[
351-
FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
351+
FakeCommand(command: <String>['git', 'fetch', '--tags']),
352352
FakeCommand(
353353
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
354354
exception: ProcessException('git', <String>[
@@ -381,7 +381,7 @@ void main() {
381381
'fetchLatestVersion throws toolExit if no upstream configured',
382382
() async {
383383
processManager.addCommands(const <FakeCommand>[
384-
FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
384+
FakeCommand(command: <String>['git', 'fetch', '--tags']),
385385
FakeCommand(
386386
command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
387387
exception: ProcessException('git', <String>[

‎packages/flutter_tools/test/general.shard/git_test.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void main() {
2222

2323
// Regression test for https://github.com/flutter/flutter/issues/74165.
2424
group('on Windows', () {
25-
const expectedCygwinEnvVars = {'MSYS': 'noglob', 'CYGWIN': 'noglob'};
25+
const expectedCygwinEnvVars = {'MSYS': 'noglob', 'CYGWIN': 'noglob', 'GIT_TERMINAL_PROMPT': '0'};
2626

2727
setUp(() {
2828
git = Git(
@@ -67,7 +67,7 @@ void main() {
6767

6868
test('git.stream passes ENV=noglob if environment is omitted', () async {
6969
processManager.addCommand(
70-
const FakeCommand(command: ['git', 'foo', 'bar'], environment: expectedCygwinEnvVars),
70+
const FakeCommand(command: ['git', 'foo', 'bar'], environment: {'MSYS': 'noglob', 'CYGWIN': 'noglob'}),
7171
);
7272
await git.stream(['foo', 'bar']);
7373
});
@@ -76,7 +76,7 @@ void main() {
7676
processManager.addCommand(
7777
const FakeCommand(
7878
command: ['git', 'foo', 'bar'],
79-
environment: {...expectedCygwinEnvVars, 'baz': 'BAZ'},
79+
environment: {'MSYS': 'noglob', 'CYGWIN': 'noglob', 'baz': 'BAZ'},
8080
),
8181
);
8282
await git.stream(['foo', 'bar'], environment: {'baz': 'BAZ'});

‎packages/flutter_tools/test/general.shard/version_test.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void main() {
167167
],
168168
stdout: getChannelUpToDateVersion().toString(),
169169
),
170-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
170+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
171171
FakeCommand(
172172
command: const <String>[
173173
'git',
@@ -367,7 +367,7 @@ void main() {
367367
],
368368
stdout: getChannelUpToDateVersion().toString(),
369369
),
370-
const FakeCommand(command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', '--tags']),
370+
const FakeCommand(command: <String>['git', 'fetch', '--tags']),
371371
FakeCommand(
372372
command: const <String>[
373373
'git',
@@ -1483,7 +1483,7 @@ void main() {
14831483
stdout: 'master',
14841484
),
14851485
const FakeCommand(
1486-
command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', 'https://github.com/flutter/flutter.git', '--tags', '-f'],
1486+
command: <String>['git', 'fetch', 'https://github.com/flutter/flutter.git', '--tags', '-f'],
14871487
),
14881488
const FakeCommand(command: <String>['git', 'tag', '--points-at', 'HEAD']),
14891489
...mockGitTagHistory(
@@ -1506,7 +1506,7 @@ void main() {
15061506
stdout: 'master',
15071507
),
15081508
const FakeCommand(
1509-
command: <String>['git', '-c', 'core.sshCommand=ssh -o BatchMode=yes', 'fetch', 'https://githubmirror.com/flutter.git', '--tags', '-f'],
1509+
command: <String>['git', 'fetch', 'https://githubmirror.com/flutter.git', '--tags', '-f'],
15101510
),
15111511
const FakeCommand(command: <String>['git', 'tag', '--points-at', 'HEAD']),
15121512
...mockGitTagHistory(

0 commit comments

Comments
 (0)