Skip to content

Dialog response await will never continue if reuse same instance two times, but works OK if another instance shown between #384

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

Closed
KirillBorunov opened this issue Nov 9, 2021 · 2 comments · Fixed by #389

Comments

@KirillBorunov
Copy link
Contributor

KirillBorunov commented Nov 9, 2021

Let say we have 2 dialog instances:

var dialogA = new...
var dialogB = new...

Now this code will fail (the dialog A will show two times, but second await will never continue):

await dialogA.ShowAsync(player);
await dialogA.ShowAsync(player);

But this one will work as expected:

await dialogA.ShowAsync(player);
await dialogB.ShowAsync(player);
await dialogA.ShowAsync(player);
@KirillBorunov
Copy link
Contributor Author

KirillBorunov commented Nov 11, 2021

The problem seems to be here:

public virtual void Fire(TKey key, TArguments arguments)
{
if (key == null) throw new ArgumentNullException(nameof(key));
if (!_completionSources.ContainsKey(key)) return;
_completionSources[key].SetResult(arguments);
Remove(key);
}

The user shows dialogA and awaits response.
The _completionSources is populated with a task.
When the dialog is responded, the line 67 will "release" the user await, and the user code will execute (continue after the await).
The ASyncWaiter code will "suspend" on the line 67 until the next user await.
The user shows again the same dialog instance and awaits again for the response.
The _completionSources is populated with a new task for the new dialog show.
The ASyncWaiter code will "continue" to the line 68 and execute the .Remove().
The _completionSources is cleared.
When the new dialog is responded, the line 65 will return and the second await will never "continue".

What if we swap the lines 67 and 68?
Will it cause other problems? Was it put in that order intentionally?
I will test and report the results.

@KirillBorunov KirillBorunov changed the title Dialog not shown if reuse same instance two times, but shown if another instance shown between Dialog response await will never continue if reuse same instance two times, but works OK if another instance shown between Nov 11, 2021
@KirillBorunov
Copy link
Contributor Author

I changed the lines 67-68 to:

var task = _completionSources[key];
Remove(key);
task.SetResult(arguments);

And 80-81 to:

var task = _completionSources[key];
Remove(key);
task.TrySetCanceled();

And the problem gone, all seems to work all OK, I will test more time and report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant