Skip to content

test_pthread_run_on_main_thread seems to be causing chrome to stop responding #18210

Closed

Description

@sbc100

I'm not sure what changed .. perhaps the version of chrome we test under.

Activity

  1. kripken commented on Nov 16, 2022

    @kripken
    Member

    Does it fail consistently, or randomly?

  2. sbc100 commented on Nov 16, 2022

    @sbc100
    CollaboratorAuthor

    Not consistently, but I was seeing it fail more often than not in the last day or so. For example, I saw it fail 4 or 5 times in a row on a single PR, and at the same time file across 5 separate PRs, but at the same time of those PRs did eventually land.

  3. sbc100 commented on Dec 3, 2022

    @sbc100
    CollaboratorAuthor

    Seems like maybe this was effecting firefox too: #18307

  4. sbc100 commented on Feb 8, 2024

    @sbc100
    CollaboratorAuthor

    I was trying to make this test pass under node and I found the cause of the deadlock. The following simple program wil consistently reproduce the issue under node:

    #include <emscripten/threading.h>                                                
    #include <stdio.h>                                                               
    #include <assert.h>                                                              
    #include <math.h>                                                                
                                                                                     
    void v() {                                                                       
      assert(emscripten_is_main_runtime_thread());                                   
      printf("Hello!\n");                                                            
    }                                                                                
                                                                                     
    void *thread_main(void*) {                                                       
      // Make the main thread call printf                                            
      emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_V, v);                 
                                                                                     
      // Now call printf outselves.                                                  
      printf("never see this\n");                                                                                              
                                                                                     
      return 0;                                                                      
    }                                                                                
                                                                                     
    int main() {                                                                     
      pthread_t thread;                                                              
      int rc = pthread_create(&thread, NULL, thread_main, NULL);                     
      assert(rc == 0);                                                               
      rc = pthread_join(thread, 0);                                                  
      assert(rc == 0);                                                               
      return 0;                                                                      
    }      
    

    The problem comes down to this:

    1. main thread spawns worker thread and then blocked until its done
    2. main thread "blocks" by waking up ever 1ms to check for tasks
    3. worker thread sends v as an async task for main thread
    4. before main thread wakes up, worker thread calls printf which essentially does:
      • acquires stdout lock
      • syncronously proxy fd_write syscall to main thread (worker thread never returns from this)
      • release stdout lock
    5. main thread wakes up and starts performing its first task which is v
    6. main thread calls printf as part of running v
    7. main thread attempt to acquire the printf lock of the worker thread is holding it.
    8. main thread waits forever, and does not perform any more tasks because we don't run tasks recursively
  5. sbc100 commented on Feb 8, 2024

    @sbc100
    CollaboratorAuthor

    @tlively and I discussed the issue but cannot find a reasonable fix. For now I think I will re-enable the test but use -sPROXY_TO_PTHREAD to avoid the deadlock.

  6. sbc100 commented on Feb 8, 2024

    @sbc100
    CollaboratorAuthor

    Actually PROXY_TO_PTHREAD doesn't make any difference. Replacing printf with emscripten_outf works around it though.

  7. added 6 commits that reference this issue on Feb 8, 2024
    e51b97b
    63fe92c
    4a9b5a9
    9da7880
    b8125e0
    c84ebdc
  8. added a commit that references this issue on Feb 8, 2024
    3bb85de
  9. added a commit that references this issue on Feb 23, 2024
    baa8d48
  10. added a commit that references this issue on Apr 12, 2024
    baa07c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions