Git Product home page Git Product logo

Comments (9)

Place1 avatar Place1 commented on July 19, 2024

So I think this could be an issue caused by a blocked Javascript event loop.
The app.init() call hits g_application_run() see source and this C function blocks until the UI is closed. Because a Javascript function is calling into this, that javascript function app.init() will be blocked as well. Because Node is single threaded this stops all Node code, such as pending promises or setTimeout callbacks, from running!

Take this example:

import { App, Button } from '../../lib';

const app = new App({
  title: 'Node Gtk'
});
app.init((window) => {
  const button = new Button({
    name: 'Button 1'
  });
  button.attach(window);
  console.log('creating a set timeout');
  setTimeout(() => console.log('this will run after the application window closes'), 1000);
  app.render();
});

console.log('I will run after the application window is closed!');

Output:

creating a set timeout
// nothing will happen...
// now after the UI is closed by the user...
I will run after the application window is closed!
this will run 1 second after the application window closes

from gtk-node.

clayrisser avatar clayrisser commented on July 19, 2024

Wow, super inciteful. This can definitely be fixed.

from gtk-node.

clayrisser avatar clayrisser commented on July 19, 2024

I fixed it with c75dab5

I'm now using promises instead of callbacks

from gtk-node.

Place1 avatar Place1 commented on July 19, 2024

I'm not sure if this commit fixes the issue. I'm getting a seg-fault running the demo now but besides that, there's still a blocking piece of C code.

source

int init(GtkApplication *app) {
  g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
  int status = g_application_run(G_APPLICATION(app), 0, NULL);
  g_object_unref(app);
  return status;
}

the g_application_run(G_APPLICATION(app), 0, NULL); method blocks indefinetly until the UI is closed.

EDIT:
in regards to the seg fault, I put a 'console.log()' at the start of the demo and that appears to result in a different error:

/home/james/react-gtk3-project/node-gtk3/node_modules/ffi/lib/callback.js:18
        throw new Error(err)
        ^

Error: ffi fatal: callback has been garbage collected!
    at /home/james/react-gtk3-project/node-gtk3/node_modules/ffi/lib/callback.js:18:15
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

So i guess there's some sort of race condition happening.

from gtk-node.

Place1 avatar Place1 commented on July 19, 2024

i'm working on a fix

from gtk-node.

Place1 avatar Place1 commented on July 19, 2024

Place1@22ac86b
Place1@134758b

This is were i'm up to. Promises, callbacks, setTimouts etc. all work now, but the program seg faults after a few seconds unfortuantly. I've added some comments on why I think this happens

init() {
    loop.startLoop();
    return new Promise((resolve, reject) => {
      // assign the ffi callback to 'this' so we retain a reference
      // after this.init() finishes. If we don't do this Node
      // will garbage collect it and we will get a segmentation
      // fault when C calls into it.
      this.onActivate = ffi.Callback('void', [type.GtkWidgetPtr], (windowPtr) => {
        this.window = new Window({ pointer: windowPtr });
        resolve(this.window);
      });
      app.register_on_activate(this.onActivate);

      // If we call app.init() then it blocks the main loop
      // forever and out program halts.
      // If we call app.init.async() then FFI will call the
      // foreign function in another thread (https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial#async-library-calls)
      // My guess is that it will be scheduled on one of the
      // threads that LibUV controls for IO tasks.
      // This means we avoid blocking the event loop but this
      // results in a segmentation fault after some time and
      // I can't figure out why. My only guess is that it's a
      // multi-thread issue with GTK???
      app.init.async(this.pointer, () => null);
    });
  }

from gtk-node.

clayrisser avatar clayrisser commented on July 19, 2024

The fix for the blocking event loop was not a change in the C code. It was a change in how I called the init function.

// blocking app.js
      app.init(this.pointer);
// unblocking app.js
      app.init.async(this.pointer, (err, status) => {
        if (status !== 0) {
          console.error(new Error('Program initialized with error'));
        }
        process.exit(status);
      });

from gtk-node.

clayrisser avatar clayrisser commented on July 19, 2024

Let's move the seg-fault to a new issue.

from gtk-node.

clayrisser avatar clayrisser commented on July 19, 2024

I filed a new issue for this at #6

from gtk-node.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.