Skip to content

Promises sometimes don't get called #390

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
rotu opened this issue Oct 8, 2019 · 5 comments · Fixed by #400
Closed

Promises sometimes don't get called #390

rotu opened this issue Oct 8, 2019 · 5 comments · Fixed by #400

Comments

@rotu
Copy link
Contributor

rotu commented Oct 8, 2019

Specification

  • pywebview version: 3.0.2
  • platform / version: MacOS Catalina

Description

Initiate a function call from python to javascript multiple times. Observe that the function does get called multiple times but only one value is returned to Javascript via promise.

    for (let i = 0; i < 10; i++) {
        console.log('request ' + i);
        api.echo(i).then((j) => {
            console.log('response ' + i)
        })
    }
class JSApi:
    def echo(self, value):
        print(value)
        return value

Javascript output:

[Log] request 0 (main.js, line 39)
[Log] request 1 (main.js, line 39)
[Log] request 2 (main.js, line 39)
[Log] request 3 (main.js, line 39)
[Log] request 4 (main.js, line 39)
[Log] request 5 (main.js, line 39)
[Log] request 6 (main.js, line 39)
[Log] request 7 (main.js, line 39)
[Log] request 8 (main.js, line 39)
[Log] request 9 (main.js, line 39)
[Log] response 0 (main.js, line 41)

Practicalities

  • YES I am willing to work on this issue myself.

  • NO I am not prepared to support this issue financially.

rotu added a commit to rotu/pywebview that referenced this issue Oct 9, 2019
1. when calling a Python function multiple times from Javascript, Promises will still get settled r0x0r#390
2. Add a per-browser ID so that return values are routed back to the correct client when you have multiple windows
3. allow calling python functions with multiple arguments
@r0x0r
Copy link
Owner

r0x0r commented Oct 9, 2019

It looks like a race condition is in question here. As a function level global return value is used, it gets immediately overwritten by subsequent function calls. A solution would be to differentiate function calls by uuid or something along these lines.

@rotu
Copy link
Contributor Author

rotu commented Oct 9, 2019

I wouldn’t call it a race condition but you’re spot on about the cause. I’m actively working on a fix.

rotu added a commit to rotu/pywebview that referenced this issue Oct 9, 2019
1. when calling a Python function multiple times from Javascript, Promises will still get settled r0x0r#390
2. Add a per-browser ID so that return values are routed back to the correct client when you have multiple windows
3. allow calling python functions with multiple arguments
@r0x0r
Copy link
Owner

r0x0r commented Oct 15, 2019

I have pushed a tentative fix to the js-api-fix branch. It is mostly untested, tests should be fixed and Windows binaries should be regenerated as well.

@r0x0r
Copy link
Owner

r0x0r commented Oct 16, 2019

PR here
#400

@r0x0r
Copy link
Owner

r0x0r commented Oct 23, 2019

test_js_api::test_concurrent randomly fails on Windows. After hours of debugging I could not track the cause of the bug down. I won't spend any more time trying to fix it and let it be for the time being. Let's hope it is a bug in the assert_js and not the underlying code.

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

Successfully merging a pull request may close this issue.

2 participants