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

Propagating exceptions from JS through Python to JS has a memory leak #283

Open
mmomtchev opened this issue Oct 3, 2024 · 3 comments
Open

Comments

@mmomtchev
Copy link
Owner

The following test leaks memory:

it('catching JS exceptions propagated through Python', () => {
  const fn = PyObject.fromJS(() => {
    throw new Error('JS exception');
  });

  assert.isTrue(fn.callable);
  assert.equal(fn.type, 'pymport.js_function');

  const py_catch = pymport('python_helpers').get('dont_catch_exception');

  assert.throws(() => {
    py_catch.call(fn);
  }, /Python exception: JS exception/);
});
@mmomtchev
Copy link
Owner Author

mmomtchev commented Oct 3, 2024

Happens only with the fastcall protocol on Python > 3.9 V8 does not garbage collect fn

@mmomtchev
Copy link
Owner Author

mmomtchev commented Oct 3, 2024

This test does not have the leak:

it'catching JS exceptions propagated through Python', () => {
  const fn = PyObject.fromJS(() => {
    throw new Error('JS exception');
  });

  assert.isTrue(fn.callable);
  assert.equal(fn.type, 'pymport.js_function');

  const py_dont_catch = pymport('python_helpers').get('dont_catch_exception');

  try {
    py_dont_catch.call(fn);
  } catch { }
});

@mmomtchev
Copy link
Owner Author

However this one does:

it('catching JS exceptions propagated through Python', () => {
  const fn = PyObject.fromJS(() => {
    throw new Error('JS exception');
  });

  assert.isTrue(fn.callable);
  assert.equal(fn.type, 'pymport.js_function');

  const py_dont_catch = pymport('python_helpers').get('dont_catch_exception');

  try {
    (() => {
      py_dont_catch.call(fn);
    })();
  } catch (e: any) {
    assert.match(e.toString(), /Python exception: JS exception/);
  }
});

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

No branches or pull requests

1 participant