Skip to content

Commit

Permalink
fix(hook): fix potential data race in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
c0nstexpr committed Mar 26, 2024
1 parent acda804 commit fdc140d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Turnbind/Action/InputAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public record struct KeyState(InputKey Key, bool Pressed);

readonly HookProc m_hookProc;

readonly SpinLock m_spinLock = new();

public InputAction()
{
m_hookProc = OnKeyboard;
Expand Down Expand Up @@ -159,6 +161,15 @@ public IObservable<bool> SubscribeKeys(InputKeys keys)

void OnKey(KBDLLHOOKSTRUCT keyPtr, bool pressed)
{
{
var lockTaken = false;
m_spinLock.TryEnter(0, ref lockTaken);

if (!lockTaken) return;

if (!lockTaken || m_input.IsDisposed) return;
}

var input = keyPtr.VkCode;

UpdateModifiers(input); // Modifier key released event need to be handled manually
Expand All @@ -175,11 +186,22 @@ void OnKey(KBDLLHOOKSTRUCT keyPtr, bool pressed)

m_input.OnNext(LatestKeyState);
m_pressedKeys[input] = pressed;

m_spinLock.Exit();
}

public void Dispose()
{
UnhookWindowsHookEx(m_keyboardHook);

{
var lockTaken = false;
m_spinLock.TryEnter(ref lockTaken);

if (!lockTaken) return;
}

m_input.Dispose();
m_spinLock.Exit();
}
}
20 changes: 19 additions & 1 deletion Turnbind/Action/ProcessWindowAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ uint dwFlags
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool UnhookWinEvent(nint hWinEventHook);

readonly SpinLock m_spinLock = new();

public ProcessWindowAction()
{
const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
Expand Down Expand Up @@ -76,7 +78,12 @@ void OnFocused(
uint dwmsEventTime
)
{
if (m_focused.IsDisposed) return;
{
var lockTaken = false;
m_spinLock.TryEnter(0, ref lockTaken);

if (!lockTaken || m_focused.IsDisposed) return;
}

var focusd = m_focused.Value;

Expand All @@ -90,6 +97,8 @@ uint dwmsEventTime
m_log.LogInformation("Window lost focuse");
m_focused.OnNext(false);
}

m_spinLock.Exit();
}

bool ContainsWin(nint hwnd)
Expand Down Expand Up @@ -128,6 +137,15 @@ bool ContainsWin(nint hwnd)
public void Dispose()
{
UnhookWinEvent(m_focusedHook);

{
var lockTaken = false;
m_spinLock.TryEnter(ref lockTaken);

if (!lockTaken) return;
}

m_focused.Dispose();
m_spinLock.Exit();
}
}
Binary file modified Turnbind/Assets/logo-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions Turnbind/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using Turnbind.Action;
using Turnbind.Model;

using Wpf.Ui.Controls;

namespace Turnbind.ViewModel;

internal partial class MainWindowViewModel : ObservableValidator, IDisposable
Expand Down

0 comments on commit fdc140d

Please sign in to comment.