Skip to content

[Bug]: callback didn't executed with process.nextTick and setImmediate #30153

Closed

Description

@dogeFu

Preflight Checklist

Electron Version

13.1.4

What operating system are you using?

Windows

Operating System Version

10

What arch are you using?

x64

Last Known Working Electron version

not

Expected Behavior

callbacks added by setImmediate and process.nextTick executed normally.

Actual Behavior

After I reload my project with ctrl +R,something was wrong.
And I run those code in dev tool's console.

setImmediate(()=>{
   console.log("immediate")
})
process.nextTick(()=>{
   console.log("nextTick")
})

but console.log didn't show the log,and callback added by setTimeout works at the same time.

I read the source code,found some comment

// setImmediate and process.nextTick makes use of uv_check and uv_prepare to
// run the callbacks, however since we only run uv loop on requests, the
// callbacks wouldn't be called until something else activated the uv loop,
// which would delay the callbacks for arbitrary long time. So we should
// initiatively activate the uv loop once setImmediate and process.nextTick is
// called.

Is electron use different event loop from nodejs?
Any idea what's going on?

Testcase Gist URL

No response

Additional Information

This bug is occasional.

Activity

  1. koush commented on Jul 17, 2021

    @koush

    I'm not sure if this is related, but I've similarly seen the renderer process failing to run the event loop until it is "jogged".

    #27127

  2. codebytere commented on Jul 22, 2021

    @codebytere
    Member

    @dogeFu Tried running https://gist.github.com/c1877f611a01f9f9c56b5b8dd7de1ce3 on windows and cannot reproduce the bug. Can you please confirm that the bug is present with those settings or provide a reproducible sample yourself?

  3. dogeFu commented on Jul 23, 2021

    @dogeFu
    Author

    @dogeFu Tried running https://gist.github.com/c1877f611a01f9f9c56b5b8dd7de1ce3 on windows and cannot reproduce the bug. Can you please confirm that the bug is present with those settings or provide a reproducible sample yourself?

    The settings you provided work.
    I got this issue in my project,which's quiet large,and it happened occasionally.
    I haven't find a reproducible sample yet.
    I read the source code of nextTick and setImmediate,next time I got this problem,I will try to debug it in dev tool.Hope to provide more useful information.

  4. dogeFu commented on Jul 26, 2021

    @dogeFu
    Author

    Hi,I got this issue again.This is the new information I have:
    Funtion “processImmediate” in internal/timers.js file, was not called when I called setImmediate in dev tool. It will be called when setImmediate works.

    I thought maybe something was wrong in c++ code.Can I get this TraceEventScope data?like log in a file. I guess somehow this function return before it calls the immediate_callback_function.

    void Environment::CheckImmediate(uv_check_t* handle) {
      Environment* env = Environment::from_immediate_check_handle(handle);
      TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
                                  "CheckImmediate", env);
    
      HandleScope scope(env->isolate());
      Context::Scope context_scope(env->context());
    
      env->RunAndClearNativeImmediates();
    
      if (env->immediate_info()->count() == 0 || !env->can_call_into_js())
        return;
    
      do {
        MakeCallback(env->isolate(),
                     env->process_object(),
                     env->immediate_callback_function(),
                     0,
                     nullptr,
                     {0, 0}).ToLocalChecked();
      } while (env->immediate_info()->has_outstanding() && env->can_call_into_js());
    
  5. bpasero commented on Nov 26, 2021

    @bpasero
    Contributor

    This seems to reproduce even with Electron 16 and blocks our progress adopting newer Electron build or enabling process reuse.

    //cc @deepak1556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions