fix: use channel threadlocal instead of a ThreadStatic

This commit is contained in:
Kaz Wolfe 2025-08-25 13:31:05 -07:00
parent b18b8b40e5
commit 8cced4c1d7
No known key found for this signature in database
GPG key ID: 258813F53A16EBB4
2 changed files with 22 additions and 7 deletions

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Threading;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Ipc.Exceptions;
@ -17,6 +18,8 @@ namespace Dalamud.Plugin.Ipc.Internal;
/// </summary>
internal class CallGateChannel
{
private readonly ThreadLocal<IpcContext> ipcExecutionContext = new();
/// <summary>
/// The actual storage.
/// </summary>
@ -146,6 +149,21 @@ internal class CallGateChannel
return (TRet)result;
}
internal void SetInvocationContext(IpcContext ipcContext)
{
this.ipcExecutionContext.Value = ipcContext;
}
internal IpcContext? GetInvocationContext()
{
return this.ipcExecutionContext.IsValueCreated ? this.ipcExecutionContext.Value : null;
}
internal void ClearInvocationContext()
{
this.ipcExecutionContext.Value = null;
}
private void CheckAndConvertArgs(object?[]? args, MethodInfo methodInfo)
{
var paramTypes = methodInfo.GetParameters()

View file

@ -14,9 +14,6 @@ namespace Dalamud.Plugin.Ipc.Internal;
/// </summary>
internal abstract class CallGatePubSubBase
{
[ThreadStatic]
private static IpcContext? ipcExecutionContext;
/// <summary>
/// Initializes a new instance of the <see cref="CallGatePubSubBase"/> class.
/// </summary>
@ -77,7 +74,7 @@ internal abstract class CallGatePubSubBase
/// <returns>Returns a potential IPC context.</returns>
public IpcContext? GetContext()
{
return ipcExecutionContext;
return this.Channel.GetInvocationContext();
}
/// <summary>
@ -172,11 +169,11 @@ internal abstract class CallGatePubSubBase
private IDisposable BuildContext()
{
ipcExecutionContext = new IpcContext
this.Channel.SetInvocationContext(new IpcContext
{
SourcePlugin = this.OwningPlugin != null ? new ExposedPlugin(this.OwningPlugin) : null,
};
});
return Disposable.Create(() => { ipcExecutionContext = null; });
return Disposable.Create(() => { this.Channel.ClearInvocationContext(); });
}
}