Hi,

The code below (found in src/gnuwin32/system.c) is almost
guaranteed to do the wrong thing if 2 Rterm processes are
started at the same time (or less than 1 second apart, the
resolution of time() being 1 second):

  /* tmpfile() seems not to work on Vista: it tries to write in c:/
  ifp = tmpfile();
  */
  {
      char *tm;
      tm = getenv("TMPDIR");
      if (!isDir(tm)) {
          tm = getenv("TMP");
          if (!isDir(tm)) {
              tm = getenv("TEMP");
              if (!isDir(tm))
                  tm = getenv("R_USER"); /* this one will succeed */
          }
      }
      srand( (unsigned) time(NULL) );
      sprintf(ifile, "%s/Rscript%x%x", tm, rand(), rand());
      ifp = fopen(ifile, "w+b");
if(!ifp) R_Suicide(_("creation of tmpfile failed -- set TMPDIR suitably?"));
  }

Because the seed is set to whatever is returned by time(), it's
very easy to have 2 concurrent Rterm processes end up using the
same temp Rscript file. Note that calling rand() twice doesn't
help: if the 2 processes use the same seed, then the 2 consecutive
calls to rand() will generate the same pairs of numbers for the
2 processes.

An unfortunate consequence of this temp Rscript file collision
could very well be what we observe here:

  https://stat.ethz.ch/pipermail/r-devel/2010-September/058464.html

Could the PID be used instead? Something like this:

  sprintf(ifile, "%s/Rscript%dx%x", tm, getpid(), rand());

will surely be much safer. Furthermore, my understanding is that
a given Rterm process needs to create at most 1 temp Rscript file
so maybe using rand() is not even needed.

Note that Unix is safe because tmpfile() is used there (file
src/unix/system.c).

Cheers,
H.

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to