fix: Infinite loading screen (#216)

* Fix various issues that cause infinite loading

* Revert nullable
This commit is contained in:
hulkhan22
2025-05-01 09:18:10 +02:00
committed by GitHub
parent 2393619dac
commit 247c0b77b1
3 changed files with 37 additions and 20 deletions
@@ -7,20 +7,17 @@ namespace SPTarkov.Server.Core.Context;
[Injectable(InjectionType.Singleton)]
public class ApplicationContext
{
protected const short MaxSavedValues = 10;
private const short MaxSavedValues = 10;
private static ApplicationContext? _applicationContext;
private readonly ISptLogger<ApplicationContext> _logger;
protected readonly ConcurrentDictionary<ContextVariableType, LinkedList<ContextVariable>> variables = new();
private readonly ConcurrentDictionary<ContextVariableType, (Lock lockObject, LinkedList<ContextVariable> values)> _variables = new();
/// <summary>
/// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it
/// statically for harmony patches!
/// </summary>
public ApplicationContext
(
ISptLogger<ApplicationContext> logger
)
public ApplicationContext(ISptLogger<ApplicationContext> logger)
{
_logger = logger;
_applicationContext = this;
@@ -33,9 +30,12 @@ public class ApplicationContext
public ContextVariable? GetLatestValue(ContextVariableType type)
{
if (variables.TryGetValue(type, out var savedValues))
if (_variables.TryGetValue(type, out var savedValues))
{
return savedValues.Last!.Value;
lock (savedValues.lockObject)
{
return savedValues.values.Last!.Value;
}
}
return null;
@@ -44,9 +44,12 @@ public class ApplicationContext
public ICollection<ContextVariable> GetValues(ContextVariableType type)
{
var values = new List<ContextVariable>();
if (variables.TryGetValue(type, out var savedValues))
if (_variables.TryGetValue(type, out var savedValues))
{
values.AddRange(savedValues);
lock (savedValues.lockObject)
{
values.AddRange(savedValues.Item2);
}
}
return values;
@@ -54,26 +57,30 @@ public class ApplicationContext
public void AddValue(ContextVariableType type, object value)
{
if (!variables.TryGetValue(type, out var savedValues))
if (!_variables.TryGetValue(type, out var savedValues))
{
savedValues = [];
if (!variables.TryAdd(type, savedValues))
savedValues = new ValueTuple<Lock, LinkedList<ContextVariable>>();
savedValues.lockObject = new Lock();
savedValues.values = [];
if (!_variables.TryAdd(type, savedValues))
{
_logger.Error($"Unable to add context variable type: {type}");
}
}
if (savedValues.Count >= MaxSavedValues)
lock (savedValues.lockObject)
{
savedValues.RemoveFirst();
if (savedValues.values.Count >= MaxSavedValues)
{
savedValues.values.RemoveFirst();
}
savedValues.values.AddLast(new ContextVariable(value, type));
}
savedValues.AddLast(new ContextVariable(value, type));
}
public void ClearValues(ContextVariableType type)
{
if (!variables.Remove(type, out _))
if (!_variables.Remove(type, out _))
{
_logger.Error($"Unable to clear context variable type: {type}");
}
@@ -88,7 +88,7 @@ public class SptHttpListener : IHttpListener
while (readTask.Result != 0)
{
readBytes += readTask.Result;
totalRead.AddRange(memory.ToArray());
totalRead.AddRange(memory[..readTask.Result].ToArray());
memory = new Memory<byte>(new byte[BodyReadBufferSize]);
readTask = req.Body.ReadAsync(memory).AsTask();
readTask.Wait();
@@ -88,7 +88,16 @@ public class HttpServer(
context.Request.Cookies.TryGetValue("PHPSESSID", out var sessionId);
if (sessionId != null)
{
_applicationContext.AddValue(ContextVariableType.SESSION_ID, sessionId);
try
{
_applicationContext.AddValue(ContextVariableType.SESSION_ID, sessionId);
}
catch (Exception ex)
{
_logger.Debug("Error while adding context value: " + ex.Message);
_logger.Critical(ex.StackTrace);
throw;
}
}
// Extract header for original IP detection
@@ -106,6 +115,7 @@ public class HttpServer(
}
catch (Exception ex)
{
_logger.Debug("Error handling request: " + context.Request.Path);
_logger.Critical(ex.Message);
_logger.Critical(ex.StackTrace);
}