Skip to content
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

[Bug] 安装游戏完成后,偶发性按钮不变成开始游戏等奇怪问题 #1083

Closed
1 task done
iamscottxu opened this issue Sep 8, 2024 · 3 comments
Closed
1 task done

Comments

@iamscottxu
Copy link
Contributor

iamscottxu commented Sep 8, 2024

Checklist

  • My issue was not mentioned by others, and it is not a duplicate issue.

Description

InstallGameService.cs:

#1082,可能会造成此问题外,protected void StartTask(InstallGameState state)方法中ExecuteTaskItemAsync调用时不等待,而直接返回,且protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = default)方法中在finally中使用判断_concurrentExecuteThreadCount是否等于0的机制也会造成本该执行一次的private void CurrentTaskFinished()方法被反复执行。而CurrentTaskFinished方法的代码逻辑线程不安全,造成”安装游戏完成后,偶发性按钮不变成开始游戏等奇怪问题“。

会反复执行的原因在于,C#中的异步方法如果不进行特殊处理,调用后就会被立即执行,即在StartTask中循环还没有执行完成,前面启动的ExecuteTaskItemAsync异步任务已经在执行当中了,这会导致当任务执行速度较快时,启动异步任务的循环还没有结束,这样线程计数器_concurrentExecuteThreadCount多次等于0,造成CurrentTaskFinished方法被反复执行。

解决方法是,使用Task.WhenAll方法等待所有任务执行完成后,再调用CurrentTaskFinished方法。

_ = RunTasksAsync(); //不需要ConfigureAwait,因为返回值丢弃,且无需调用“.GetAwaiter().OnCompleted()”
return;

async Task RunTasksAsync()
{
    _cancellationTokenSource = new CancellationTokenSource();
    var tasks = new Task[Environment.ProcessorCount];
    for (int i = 0; i < Environment.ProcessorCount; i++)
    {
        tasks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token);
    }
    try
    {
        await Task.WhenAll(tasks).ConfigureAwait(false);
    }
    finally
    {
        CurrentTaskFinished();
    }
}

Reproduction Steps

protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = default)中的带有await调用的代码清空,让其快速执行完成,即可相对稳定地复现此问题。

Expected Behavior

No response

Screenshots

No response

Starward Version

main分支:37253b8

Windows Version

与操作系统版本无关

Log

No response

Additional Context

已经提pr #1084 修复。

@iamscottxu
Copy link
Contributor Author

#1084

@Scighost
Copy link
Owner

Scighost commented Sep 8, 2024

很可惜,修改后的代码没能通过测试,一键修复过程完成校验后,无法进入到下载流程。

@Scighost Scighost added Area: Install Game and removed triage Issue needs to be triaged labels Sep 8, 2024
@iamscottxu
Copy link
Contributor Author

iamscottxu commented Sep 8, 2024

抱歉,无法进入下载流程的原因是没有考虑到StartTask会被多次执行,加了一行State == None的判断逻辑导致的,修改后使用_concurrentExecuteThreadCount > 0进行判断,达到同样的效果,解决此问题。

Scighost pushed a commit that referenced this issue Sep 15, 2024
…tiple times by multiple threads (#1084)

* fixed the bug that method `CurrentTaskFinished` was executed multiple times by multiple threads (#1083) (#1082)

Signed-off-by: OsakaRuma <[email protected]>

* handling exception, deleted voice code

Signed-off-by: OsakaRuma <[email protected]>

* fixed typo of variable name

Signed-off-by: OsakaRuma <[email protected]>

* fixed typo in variable name

Signed-off-by: OsakaRuma <[email protected]>

* fixed a bug where only the first step of the task could be run if there were multiple steps of the task when installing the game

Signed-off-by: OsakaRuma <[email protected]>

---------

Signed-off-by: OsakaRuma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants