<div dir="ltr"><br>Hello, <br><br><span style="color:rgb(35,38,41);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Liberation Sans",sans-serif;font-size:15px">I am asking this question in this forum as I had already asked this question in stack overflow and did not receive any answer/comment:</span><div><a href="https://stackoverflow.com/questions/70745740/how-to-gracefully-exit-an-mpi-server-process-when-it-is-waiting-on-mpi-comm-acce">https://stackoverflow.com/questions/70745740/how-to-gracefully-exit-an-mpi-server-process-when-it-is-waiting-on-mpi-comm-acce</a><span style="color:rgb(35,38,41);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Liberation Sans",sans-serif;font-size:15px"><br></span></div><div><br></div><div><div class="gmail-votecell gmail-post-layout--left" style="margin:0px;padding:0px 16px 0px 0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Liberation Sans",sans-serif;font-size:13px;vertical-align:top;box-sizing:inherit;width:auto;color:rgb(35,38,41)"><div class="gmail-js-voting-container gmail-d-flex gmail-jc-center gmail-fd-column gmail-ai-stretch gmail-gs4 gmail-fc-black-200" style="padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;display:flex"><div class="gmail-js-vote-count gmail-flex--item gmail-d-flex gmail-fd-column gmail-ai-center gmail-fc-black-500 gmail-fs-title" style="margin:2px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;font-size:1.61538rem;display:flex"><span style="font-family:inherit;font-style:inherit;font-variant-ligatures:inherit;font-variant-caps:inherit;font-weight:inherit;font-size:15px">My concern is as follows:<br>I have an MPI server program that calls MPI_Comm_accept, in an infinite loop, on a separate thread. The main thread spawns this thread and in parallel does some other work. At some point of time the main thread decides to exit. It closes the port using MPI_Close_port(portName), however the MPI_Comm_accept() is still waiting for connection requests.</span></div></div></div><div class="gmail-postcell gmail-post-layout--right" style="margin:0px;padding:0px 16px 0px 0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Liberation Sans",sans-serif;font-size:13px;vertical-align:top;box-sizing:inherit;width:auto;min-width:0px;color:rgb(35,38,41)"><div class="gmail-s-prose gmail-js-post-body" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;width:659px"><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;clear:both"><br>My question is:<br>- How does the main thread exit gracefully? Is there an equivalent of socket close in MPI? I am using mpich which I can use from main thread?</p><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">- Is there anyway I can set timeout to the MPI_Comm_accept call?</p><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">Here is a dummy code that looks similar to my server code:</p><pre style="margin-top:0px;margin-bottom:0px;padding:12px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:1.30769;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:5px"><code style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit;border-radius:0px">// MPI Server Program

void accept(std::string portName) {

  while (true) {
    MPI_Comm intercomm;
    MPI_Comm_accept(portName.c_str(), MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm);    
    // handle connection
    MPI_Comm_disconnect(&intercomm);    
  }
}

int main() {   

  char ch; 
  MPI_Init(NULL, NULL);
  char portName[MPI_MAX_PORT_NAME];    
  MPI_Open_port(MPI_INFO_NULL, portName);    
  publishName("nameServerFile.txt", "ocean", std::string(portName));

  std::thread th(&accept, std::string(portName));

  // do something

  MPI_Close_port(portName);    
  std::cout <<"\nClosed port" << std::endl;
  th.join();       // main thread waits infinitely at this join 
                   // as child thread is still waiting on MPI_Comm_accept
  MPI_Finalize();   
}</code></pre></div></div></div><div><br></div></div>