Serialize player context mutations against the periodic save
A player's progress is persisted from two unrelated flows: action handlers triggered by incoming packets (sequential per connection) and the periodic save, which runs on an independent timer. Action handlers mutate tracked entities with plain field and collection writes - for example crafting toggles item.ItemOptions, and item stacking and NPC selling delete item rows - which bypass the persistence context's own lock. When such a mutation runs while SaveChangesAsync enumerates the change tracker, the save throws (collection-modified, or a DbUpdateConcurrency "affected 0 rows" since the context has no concurrency tokens). SaveChanges is atomic, so every following save fails too and the whole session never persists: on relog the player rolls back, losing progress and items. Add a per-player re-entrant persistence lock and acquire it around both the packet-handling funnel and SaveProgressAsync, so a player's mutations and saves can never overlap. The lock is re-entrant per asynchronous flow (an instance AsyncLocal), so an inline save inside an already-serialized handler does not deadlock; and it is per player, so a trade still acquires the trading partner's lock separately. Add regression tests covering mutual exclusion and re-entrancy of the lock.
This commit is contained in:
@@ -54,6 +54,21 @@ public class Player : AsyncDisposable, IBucketMapObserver, IAttackable, IAttacke
|
||||
private readonly AsyncLock _moveLock = new();
|
||||
private readonly AsyncLock _experienceLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// Serializes context mutations done by this player's action handlers against the periodic and
|
||||
/// disconnect progress saves, which run on an independent timer flow. See
|
||||
/// <see cref="RunPersistenceExclusiveAsync{T}"/>.
|
||||
/// </summary>
|
||||
private readonly AsyncLock _persistenceLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// Tracks, per asynchronous flow, whether <see cref="_persistenceLock"/> is already held, so the
|
||||
/// lock can be re-entered (Nito's <see cref="AsyncLock"/> is not reentrant). It is an instance
|
||||
/// field on purpose: reentrancy must be tracked per player, so a flow holding player A's lock
|
||||
/// still acquires player B's lock (e.g. during a trade) instead of wrongly skipping it.
|
||||
/// </summary>
|
||||
private readonly AsyncLocal<bool> _persistenceLockHeld = new();
|
||||
|
||||
private readonly Walker _walker;
|
||||
|
||||
private readonly AppearanceDataAdapter _appearanceData;
|
||||
@@ -1879,12 +1894,78 @@ public class Player : AsyncDisposable, IBucketMapObserver, IAttackable, IAttacke
|
||||
/// <returns>Success of the save operation.</returns>
|
||||
public async ValueTask<bool> SaveProgressAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!this.IsTemplatePlayer)
|
||||
if (this.IsTemplatePlayer)
|
||||
{
|
||||
return await this.PersistenceContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
return await this.RunPersistenceExclusiveAsync(
|
||||
() => this.PersistenceContext.SaveChangesAsync(cancellationToken),
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the given operation while holding this player's persistence lock, so that context
|
||||
/// mutations and progress saves for the player never run concurrently.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The periodic progress save (<see cref="PlugIns.PeriodicSaveProgressPlugIn"/>) runs on an
|
||||
/// independent timer flow. Action handlers mutate tracked entities with plain field/collection
|
||||
/// writes (e.g. crafting toggling <c>item.ItemOptions</c>) which bypass the persistence context's
|
||||
/// own lock; if such a mutation runs while <see cref="IContext.SaveChangesAsync"/> enumerates the
|
||||
/// change tracker, the save throws (collection-modified / DbUpdateConcurrency) and every following
|
||||
/// save fails too, so the whole session is lost on relog. Serializing the packet handler funnel
|
||||
/// and the save against each other closes that window. The lock is re-entrant per asynchronous
|
||||
/// flow, so an inline save inside an already-serialized handler does not deadlock.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">The result type of the operation.</typeparam>
|
||||
/// <param name="operation">The operation to run exclusively.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The result of the operation.</returns>
|
||||
public async ValueTask<T> RunPersistenceExclusiveAsync<T>(Func<ValueTask<T>> operation, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (this._persistenceLockHeld.Value)
|
||||
{
|
||||
return await operation().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
using var l = await this._persistenceLock.LockAsync(cancellationToken).ConfigureAwait(false);
|
||||
this._persistenceLockHeld.Value = true;
|
||||
try
|
||||
{
|
||||
return await operation().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this._persistenceLockHeld.Value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the given operation while holding this player's persistence lock.
|
||||
/// See <see cref="RunPersistenceExclusiveAsync{T}"/> for the rationale.
|
||||
/// </summary>
|
||||
/// <param name="operation">The operation to run exclusively.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A value task which completes when the operation completed.</returns>
|
||||
public async ValueTask RunPersistenceExclusiveAsync(Func<ValueTask> operation, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (this._persistenceLockHeld.Value)
|
||||
{
|
||||
await operation().ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
using var l = await this._persistenceLock.LockAsync(cancellationToken).ConfigureAwait(false);
|
||||
this._persistenceLockHeld.Value = true;
|
||||
try
|
||||
{
|
||||
await operation().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this._persistenceLockHeld.Value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user