message: Fix a race condition on stream close
diff: @@ -132,6 +132,7 @@ SstStream SstReaderOpen(const char *Name, SstParams Params, MPI_Comm comm)
  Stream->CPInfo = CP_getCPInfo(Stream->DP_Interface);
  Stream->mpiComm = comm;
  + Stream->FinalTimestep = INT_MAX; /* set this on close */
  MPI_Comm_rank(Stream->mpiComm, &Stream->Rank);
  MPI_Comm_size(Stream->mpiComm, &Stream->CohortSize);
  @@ -372,6 +373,7 @@ extern void CP_WriterCloseHandler(CManager cm, CMConnection conn, void *Msg_v,
  Msg->FinalTimestep);
  pthread_mutex_lock(&Stream->DataLock);
  + Stream->FinalTimestep = Msg->FinalTimestep;
  Stream->Status = PeerClosed;
  /* wake anyone that might be waiting */
  pthread_cond_signal(&Stream->DataCondition);
  @@ -447,9 +449,14 @@ static TSMetadataList waitForNextMetadata(SstStream Stream, long LastTimestep)
  return FoundTS;
  }
  /* didn't find a good next timestep, check Stream status */
  - if (Stream->Status != Established)
  + if ((Stream->Status != Established) ||
  + ((Stream->FinalTimestep != INT_MAX) &&
  + (Stream->FinalTimestep >= LastTimestep)))
  {
  pthread_mutex_unlock(&Stream->DataLock);
  + CP_verbose(Stream,
  + "Stream Final Timestep is %d, last timestep was %d\n",
  + Stream->FinalTimestep, LastTimestep);
  CP_verbose(Stream, "Wait for next metadata returning NULL because "
  "Stream is not Established\n");
  /* closed or failed, return NULL */
  @@ -458,6 +465,7 @@ static TSMetadataList waitForNextMetadata(SstStream Stream, long LastTimestep)
  CP_verbose(Stream,
  "Waiting for metadata for a Timestep later than TS %d\n",
  LastTimestep);
  + CP_verbose(Stream, "Stream status is %d\n", Stream->Status);
  /* wait until we get the timestep metadata or something else changes */
  pthread_cond_wait(&Stream->DataCondition, &Stream->DataLock);
  }
  