Get properties that use Task.Run().Result seem to block execution #514

Closed
opened 2026-01-29 16:41:40 +00:00 by claunia · 2 comments
Owner

Originally created by @freosc on GitHub (Jun 12, 2020).

  • Version: latest from github 9.31.1

.net core 3.1 node 12.16.3

  • Target: win

Steps to Reproduce:

  1. In the demo Electron.Net WebApp, in ElectronBootstrap, I added these lines of code
Console.WriteLine("set AllowDowngrade");
Electron.AutoUpdater.AllowDowngrade = true;
Console.WriteLine("get AllowDowngrade");
//Code execution blocks on the following line
bool allowDowngrade = Electron.AutoUpdater.AllowDowngrade;
Console.WriteLine("AllowDowngrade: " + allowDowngrade);
  1. The set property works fine, but I noticed that the get property blocks execution, so the console won't show the last line in the code block above.
    Inside the property getter code I see that the code is executed correctly, but the socket.on "autoUpdater-allowDowngrade-get-reply" event is never fired.

This bug occurs for all property getters in the electron.net code that use the Task.Run().Result pattern

(Sidenote: a few seconds after the code execution is blocked, the socket also disconnects)

Originally created by @freosc on GitHub (Jun 12, 2020). <!-- Please search existing issues to avoid creating duplicates. --> <!-- Which version of Electron.NET CLI and API are you using? --> <!-- Please always try to use latest version before report. --> * **Version**: latest from github 9.31.1 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> .net core 3.1 node 12.16.3 <!-- What target are you building for? --> * **Target**: win <!-- Enter your issue details below this comment. --> <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> Steps to Reproduce: 1. In the demo Electron.Net WebApp, in ElectronBootstrap, I added these lines of code ``` Console.WriteLine("set AllowDowngrade"); Electron.AutoUpdater.AllowDowngrade = true; Console.WriteLine("get AllowDowngrade"); //Code execution blocks on the following line bool allowDowngrade = Electron.AutoUpdater.AllowDowngrade; Console.WriteLine("AllowDowngrade: " + allowDowngrade); ``` 2. The set property works fine, but I noticed that the get property blocks execution, so the console won't show the last line in the code block above. Inside the property getter code I see that the code is executed correctly, but the socket.on "autoUpdater-allowDowngrade-get-reply" event is never fired. This bug occurs for all property getters in the electron.net code that use the Task.Run().Result pattern (Sidenote: a few seconds after the code execution is blocked, the socket also disconnects)
claunia added the bug label 2026-01-29 16:41:40 +00:00
Author
Owner

@freosc commented on GitHub (Jun 14, 2020):

I discovered that it does work in sync methods, and doesn't work in async methods.
If you put the example lines of code inside the Program.CreateWebHostBuilder, like this:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var x = WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, logging) => { logging.AddConsole(); })
                .UseElectron(args)
                .UseStartup<Startup>();

            Console.WriteLine("set AllowDowngrade");
            Electron.AutoUpdater.AllowDowngrade = true;
            Console.WriteLine("get AllowDowngrade");
            //Code execution blocks on the following line
            bool allowDowngrade = Electron.AutoUpdater.AllowDowngrade;
            Console.WriteLine("AllowDowngrade: " + allowDowngrade);

            return x;
        }

This will work. But it'll fail when ran from an async method such as the async ElectronBootstrap()
in your demo webapp, or in case somewhere upstream in the stacktrace an async method is used.

I guess we're looking at the infamous async deadlock issue, described here:
https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

@freosc commented on GitHub (Jun 14, 2020): I discovered that it does work in sync methods, and doesn't work in async methods. If you put the example lines of code inside the Program.CreateWebHostBuilder, like this: ``` public static IWebHostBuilder CreateWebHostBuilder(string[] args) { var x = WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddConsole(); }) .UseElectron(args) .UseStartup<Startup>(); Console.WriteLine("set AllowDowngrade"); Electron.AutoUpdater.AllowDowngrade = true; Console.WriteLine("get AllowDowngrade"); //Code execution blocks on the following line bool allowDowngrade = Electron.AutoUpdater.AllowDowngrade; Console.WriteLine("AllowDowngrade: " + allowDowngrade); return x; } ``` This will work. But it'll fail when ran from an async method such as the `async ElectronBootstrap()` in your demo webapp, or in case somewhere upstream in the stacktrace an async method is used. I guess we're looking at the infamous async deadlock issue, described here: [https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html)
Author
Owner

@theolivenbaum commented on GitHub (May 25, 2021):

Is there anyone working on the remaining .Result uses? Happy to jump with a PR if not...

@theolivenbaum commented on GitHub (May 25, 2021): Is there anyone working on the remaining .Result uses? Happy to jump with a PR if not...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#514