tetsurom/rxqt

Segfaults in run_loop

Closed this issue · 4 comments

Hey tetsurom,
Thanks for your work on the library!

I don't know if this is an issue report or simply a misuse on my part, but I am getting some segfaults inside the run loop, given a certain threading setup:

It seems to be an interaction between firing a subject from a non-GUI thread, which is being observed on the GUI thread. This alone doesn't cause issue, until any unrelated subscription makes use of "sample_with_time" on the GUI thread, which all together seems to cause some issues and segfault.

  rxqt::run_loop rxqt_run_loop;
  rxcpp::subjects::subject<bool> test;
  rxcpp::subjects::subject<bool> test2;

  // Subject to be fired subscribes on the GUI loop
  test.get_observable()
    .observe_on(rxqt_run_loop.observe_on_run_loop())
    .subscribe([](bool) { });

  // Unrelated subject samples on the GUI loop
  test2.get_observable()
    .sample_with_time(std::chrono::milliseconds(25), rxqt_run_loop.observe_on_run_loop())
    .subscribe([](bool){ });

  // Fire first subject from another thread.
  rxcpp::observable<>::range(1, 10000)
    .observe_on(rxcpp::synchronize_new_thread())
    .subscribe([=](int) {
      test.get_subscriber().on_next(true);
    });

I have tested another alternative, where I fire the subject on the GUI thread instead, which leads to the same results:

  rxcpp::observable<>::range(1, 10000)
    .observe_on(rxcpp::synchronize_new_thread())
    .tap([=](int) {
      // Some work on the other thread
      std::this_thread::sleep_for(std::chrono::seconds(2));
    })
    .observe_on(rxqt_run_loop.observe_on_run_loop())
    .subscribe([=](int) {
      test.get_subscriber().on_next(true);
    });;

The segfault occurs at this line in the rxqt library:

if (!timer.isActive() || ms_till_task.count() < timer.remainingTime())

I did wonder if this had to do with the rxcpp bug mentioned here:
ReactiveX/RxCpp#486
but I had the feeling it was more related to the use of QTimer in rxqt_run_loop

Let me know if you have any tips or insight. Thanks!

One thing I've noticed: If I just create my own QThread, and fire the test subject with that, everything functions properly. It's only when trying to fire the test subject off an Rxcpp-spawned thread that it collides, from my experience.

Hi Noggog, I believe now this is fixed.

Thank you for the report.

Will take a look and report back on monday. 8) Thanks!

Looks good so far! Will reopen another case if anything comes up.

Thanks a lot!