Hosthook.CallAsync<T> blocks execution when exception occurs inside #508

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

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

Originally assigned to: @GregorBiswanger on GitHub.

API and CLI 8.31.2

  • Version: 8.31.2

.net core 3.1, node 12.16.3

  • Target: Windows 10

When using HostHook.CallAsync(), my call is blocked when an exception occurs inside the CallAsync method, and code after my CallAsync doesn't get executed.
I updated the on complete handler in your HostHook.CallAsync() code like this:

...
catch (Exception exception)
                {
                    taskCompletionSource.SetException(exception);
                    //Don't use the following line, as it blocks execution in the calling method
                    //throw new InvalidCastException("Return value does not match with the generic type.", exception);
                }
...

Which redirects the exception to the calling code, where I can catch it, and resolves this issue.
I guess the same "SetException" code should be used in the on error handler, to be sure that the code doesn't block the caller.

Steps to Reproduce:

  1. Inside ElectronHostHook/index.ts i have this test code:
...
onHostReady(): void
    {
        // execute your own JavaScript Host logic here
        this.on('test', async (id, done) =>
        {
            try
            {
                console.log("test called", id);
                done(id);
                console.log("test end");
            } catch (ex)
            {
                console.log("ex occurred in test", ex);
            }
        });
    }
...
  1. Inside my electron.net calling logic in .net:
...
 _mainWindow.OnReadyToShow += async () =>
                {
                    _logger.LogInformation("Browser ready to show");
                    _mainWindow.Show();
                    _logger.LogInformation("test");
                    int test = -1;
                    try
                    {
                        //code execution will block here
                        test = await Electron.HostHook.CallAsync<int>("test", 132);
                    }
                    catch (Exception ex) {
                        _logger.LogError(ex, "Error in test in OnReadyToShow");
                    }
                    _logger.LogInformation("get test {id}", test);
                };
...

The code above will block, unless the "taskCompletionSource.SetException(exception);" is used in the CallAsync() code.
In that case, I can catch the Exception that occurs (System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.). There's no issue with the exception though, that's clear, I have to use a long instead of an int.

Originally created by @freosc on GitHub (Jun 7, 2020). Originally assigned to: @GregorBiswanger on GitHub. <!-- Please search existing issues to avoid creating duplicates. --> <!-- Which version of Electron.NET CLI and API are you using? --> API and CLI 8.31.2 <!-- Please always try to use latest version before report. --> * **Version**: 8.31.2 <!-- 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**: Windows 10 <!-- Enter your issue details below this comment. --> When using HostHook.CallAsync<T>(), my call is blocked when an exception occurs inside the CallAsync method, and code after my CallAsync doesn't get executed. I updated the on complete handler in your HostHook.CallAsync<T>() code like this: ``` ... catch (Exception exception) { taskCompletionSource.SetException(exception); //Don't use the following line, as it blocks execution in the calling method //throw new InvalidCastException("Return value does not match with the generic type.", exception); } ... ``` Which redirects the exception to the calling code, where I can catch it, and resolves this issue. I guess the same "SetException" code should be used in the on error handler, to be sure that the code doesn't block the caller. <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> Steps to Reproduce: 1. Inside ElectronHostHook/index.ts i have this test code: ``` ... onHostReady(): void { // execute your own JavaScript Host logic here this.on('test', async (id, done) => { try { console.log("test called", id); done(id); console.log("test end"); } catch (ex) { console.log("ex occurred in test", ex); } }); } ... ``` 2. Inside my electron.net calling logic in .net: ``` ... _mainWindow.OnReadyToShow += async () => { _logger.LogInformation("Browser ready to show"); _mainWindow.Show(); _logger.LogInformation("test"); int test = -1; try { //code execution will block here test = await Electron.HostHook.CallAsync<int>("test", 132); } catch (Exception ex) { _logger.LogError(ex, "Error in test in OnReadyToShow"); } _logger.LogInformation("get test {id}", test); }; ... ``` The code above will block, unless the "taskCompletionSource.SetException(exception);" is used in the CallAsync<T>() code. In that case, I can catch the Exception that occurs (System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.). There's no issue with the exception though, that's clear, I have to use a long instead of an int.
claunia added the bug label 2026-01-29 16:41:29 +00:00
Author
Owner

@GregorBiswanger commented on GitHub (Jun 11, 2020):

Would you like to submit a pull request to us? Electron.NET - Contributing Getting Started

@GregorBiswanger commented on GitHub (Jun 11, 2020): Would you like to submit a pull request to us? [Electron.NET - Contributing Getting Started](https://youtu.be/Po-saU_Z6Ws)
Author
Owner

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

Sure

@freosc commented on GitHub (Jun 12, 2020): Sure
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#508