Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FileLocksmith]Fix crash while opening process handlers #21602

Merged
merged 2 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace FileLocksmith::Interop
System::String^ name;
System::UInt32 pid;
array<System::String^>^ files;
System::Boolean isExpanded; // For helping in the UI
};

System::String^ from_wstring_view(std::wstring_view str)
Expand Down Expand Up @@ -77,7 +76,6 @@ namespace FileLocksmith::Interop
{
item->files[j] = from_wstring_view(result_cpp[i].files[j]);
}
item->isExpanded = false;

result[i] = item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,49 @@ await Task.Run(() =>

private async void WatchProcess(ProcessResult process, CancellationToken token)
{
Process handle = Process.GetProcessById((int)process.pid);
try
{
await handle.WaitForExitAsync(token);
}
catch (TaskCanceledException)
{
// Nothing to do, normal operation
}
Process handle = Process.GetProcessById((int)process.pid);
try
{
await handle.WaitForExitAsync(token);
}
catch (TaskCanceledException)
{
// Nothing to do, normal operation
}

if (handle.HasExited)
if (handle.HasExited)
{
Processes.Remove(process);
}
}
catch (Exception ex)
{
Processes.Remove(process);
Logger.LogError($"Couldn't add a waiter to wait for a process to exit. PID = {process.pid} and Name = {process.name}.", ex);
Processes.Remove(process); // If we couldn't get an handle to the process or it has exited in the meanwhile, don't show it.
}
}

[RelayCommand]
public void EndTask(ProcessResult selectedProcess)
{
Process handle = Process.GetProcessById((int)selectedProcess.pid);
try
{
handle.Kill();
Process handle = Process.GetProcessById((int)selectedProcess.pid);
try
{
handle.Kill();
}
catch (Exception ex)
{
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.", ex);
}
}
catch (Exception)
catch (Exception ex)
{
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.");
Logger.LogError($"Couldn't get an handle to kill process {selectedProcess.name} with PID {selectedProcess.pid}. Likely has been killed already.", ex);
Processes.Remove(selectedProcess); // If we couldn't get an handle to the process, remove it from the list, since it's likely been killed already.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="interop:ProcessResult">
<labs:SettingsExpander IsExpanded="{Binding isExpanded}" Margin="0,3,0,0">
<labs:SettingsExpander Margin="0,3,0,0">
<labs:SettingsExpander.Header>
<!-- We can't use the HeaderIcon because it only support a BitmapIcon, which only supports UriSource - not a direct BitmapImage -->
<StackPanel Orientation="Horizontal">
Expand Down