Skip to content

Commit bd81ccd

Browse files
committed
fix: main thread deadlock cause by calling .Result directly (#1720)
Signed-off-by: leo <[email protected]>
1 parent 20ca646 commit bd81ccd

29 files changed

+470
-333
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ private void TryOpenRepository(string repo)
578578
{
579579
if (!string.IsNullOrEmpty(repo) && Directory.Exists(repo))
580580
{
581-
var test = new Commands.QueryRepositoryRootPath(repo).GetResultAsync().Result;
581+
var test = new Commands.QueryRepositoryRootPath(repo).GetResult();
582582
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
583583
{
584584
Dispatcher.UIThread.Invoke(() =>

src/Commands/Command.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ public void Exec()
5050
}
5151
}
5252

53+
protected Result ReadToEnd()
54+
{
55+
using var proc = new Process() { StartInfo = CreateGitStartInfo(true) };
56+
57+
try
58+
{
59+
proc.Start();
60+
}
61+
catch (Exception e)
62+
{
63+
return Result.Failed(e.Message);
64+
}
65+
66+
var rs = new Result() { IsSuccess = true };
67+
rs.StdOut = proc.StandardOutput.ReadToEnd();
68+
rs.StdErr = proc.StandardError.ReadToEnd();
69+
proc.WaitForExit();
70+
71+
rs.IsSuccess = proc.ExitCode == 0;
72+
return rs;
73+
}
74+
5375
public async Task<bool> ExecAsync()
5476
{
5577
Log?.AppendLine($"$ git {Args}\n");

src/Commands/Config.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ public Config(string repository)
2020
}
2121
}
2222

23+
public Dictionary<string, string> ReadAll()
24+
{
25+
Args = "config -l";
26+
27+
var output = ReadToEnd();
28+
var rs = new Dictionary<string, string>();
29+
if (output.IsSuccess)
30+
{
31+
var lines = output.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
32+
foreach (var line in lines)
33+
{
34+
var parts = line.Split('=', 2);
35+
if (parts.Length == 2)
36+
rs[parts[0]] = parts[1];
37+
}
38+
}
39+
40+
return rs;
41+
}
42+
2343
public async Task<Dictionary<string, string>> ReadAllAsync()
2444
{
2545
Args = "config -l";
@@ -40,6 +60,12 @@ public async Task<Dictionary<string, string>> ReadAllAsync()
4060
return rs;
4161
}
4262

63+
public string Get(string key)
64+
{
65+
Args = $"config {key}";
66+
return ReadToEnd().StdOut.Trim();
67+
}
68+
4369
public async Task<string> GetAsync(string key)
4470
{
4571
Args = $"config {key}";

src/Commands/IsBareRepository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ public IsBareRepository(string path)
1111
Args = "rev-parse --is-bare-repository";
1212
}
1313

14+
public bool GetResult()
15+
{
16+
if (!Directory.Exists(Path.Combine(WorkingDirectory, "refs")) ||
17+
!Directory.Exists(Path.Combine(WorkingDirectory, "objects")) ||
18+
!File.Exists(Path.Combine(WorkingDirectory, "HEAD")))
19+
return false;
20+
21+
var rs = ReadToEnd();
22+
return rs.IsSuccess && rs.StdOut.Trim() == "true";
23+
}
24+
1425
public async Task<bool> GetResultAsync()
1526
{
1627
if (!Directory.Exists(Path.Combine(WorkingDirectory, "refs")) ||

src/Commands/IsConflictResolved.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public IsConflictResolved(string repo, Models.Change change)
1313
Args = $"diff -a --ignore-cr-at-eol --check {opt}";
1414
}
1515

16+
public bool GetResult()
17+
{
18+
return ReadToEnd().IsSuccess;
19+
}
20+
1621
public async Task<bool> GetResultAsync()
1722
{
1823
var rs = await ReadToEndAsync().ConfigureAwait(false);

src/Commands/IsLFSFiltered.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ public IsLFSFiltered(string repo, string sha, string path)
2020
RaiseError = false;
2121
}
2222

23+
public bool GetResult()
24+
{
25+
return Parse(ReadToEnd());
26+
}
27+
2328
public async Task<bool> GetResultAsync()
2429
{
2530
var rs = await ReadToEndAsync().ConfigureAwait(false);
31+
return Parse(rs);
32+
}
33+
34+
private bool Parse(Result rs)
35+
{
2636
return rs.IsSuccess && rs.StdOut.Contains("filter\0lfs");
2737
}
2838
}

src/Commands/QueryCommitFullMessage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public QueryCommitFullMessage(string repo, string sha)
1111
Args = $"show --no-show-signature --format=%B -s {sha}";
1212
}
1313

14+
public string GetResult()
15+
{
16+
var rs = ReadToEnd();
17+
return rs.IsSuccess ? rs.StdOut.TrimEnd() : string.Empty;
18+
}
19+
1420
public async Task<string> GetResultAsync()
1521
{
1622
var rs = await ReadToEndAsync().ConfigureAwait(false);

src/Commands/QueryCurrentBranch.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public QueryCurrentBranch(string repo)
1111
Args = "branch --show-current";
1212
}
1313

14+
public string GetResult()
15+
{
16+
return ReadToEnd().StdOut.Trim();
17+
}
18+
1419
public async Task<string> GetResultAsync()
1520
{
1621
var rs = await ReadToEndAsync().ConfigureAwait(false);

src/Commands/QueryGitCommonDir.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.IO;
2-
using System.Threading.Tasks;
32

43
namespace SourceGit.Commands
54
{
@@ -11,9 +10,9 @@ public QueryGitCommonDir(string workDir)
1110
Args = "rev-parse --git-common-dir";
1211
}
1312

14-
public async Task<string> GetResultAsync()
13+
public string GetResult()
1514
{
16-
var rs = await ReadToEndAsync().ConfigureAwait(false);
15+
var rs = ReadToEnd();
1716
if (!rs.IsSuccess)
1817
return null;
1918

src/Commands/QueryGitDir.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ public QueryGitDir(string workDir)
1111
Args = "rev-parse --git-dir";
1212
}
1313

14+
public string GetResult()
15+
{
16+
return Parse(ReadToEnd());
17+
}
18+
1419
public async Task<string> GetResultAsync()
1520
{
1621
var rs = await ReadToEndAsync().ConfigureAwait(false);
22+
return Parse(rs);
23+
}
24+
25+
private string Parse(Result rs)
26+
{
1727
if (!rs.IsSuccess)
1828
return null;
1929

0 commit comments

Comments
 (0)