This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

1.5.25: pthread/ofstream problem


Hello,

I've been trying to track down a segmentation fault that a rather large application I'm working on has been experiencing. I have seemed to narrow it down to two threads that are opening, appending, and closing files quite often. I've created a very simple example program that suffers from the same condition.

Two threads are created, each open up their own unique file, followed by a close (no writing, in this example). The threads are both joined and then the processes is then repeated. After some indeterminate amount of time - millions of iterations - the application segfaults with no real useful information that I can see. Running in gdb doesn't seem to help (after compiling with debug flags, of course) as the backtrace is either corrupted or can't be followed because I'm not using a debug version of Cygwin.

Note that this problem seems to be accelerated by having the target directory open in explorer and/or having the files highlighted. I've come across situations where even ofstream.open() will throw an exception when doing the above. The exception has been seen in both C++ and python, which makes me think it's something fundamental in Cygwin, and possibly related to this as well.

Is there something inherently wrong with having different treads access different files at once? I have reproduced this issue across multiple machines.


Compile: g++ FileTest.cpp -lpthread -oFileTest

FileTest.cpp:
#include <fstream>
#include <string>
#include <iostream>
#include <pthread.h>

using namespace std;

struct ThreadData {
  string fileName;
};


void *
FileThread(void *arg) {
  try {

    ofstream outfile;
    ThreadData *td = (ThreadData*)arg;
    string fileName = td->fileName;

    try {
      outfile.open(fileName.c_str(), ios_base::app);
    } catch(...) {
      cerr << "Exception during open()" << endl;
      return NULL;
    }

    try {
      outfile.close();
    } catch(...) {
      cerr << "Exception during open()" << endl;
      return NULL;
    }

  } catch(...) {
    cerr << "Exception while creating objects" << endl;
    return NULL;
  }
  return NULL;
}

int
main(void) {
  unsigned long long count = 0;

  ThreadData td1;
  td1.fileName = "temp1.txt";
  ThreadData td2;
  td2.fileName = "temp2.txt";

  while(1) {
    count++;
    if(count%5000 == 0) cout << "Iteration " << count << endl;

    pthread_t thread1;
    pthread_t thread2;

    pthread_create(&thread1, NULL, FileThread, &td1);
    pthread_create(&thread2, NULL, FileThread, &td2);

    void *res = NULL;
    pthread_join(thread1, &res);
    pthread_join(thread2, &res);
  }

  // Not reached
  return 0;
}

Stackdump:
Exception: STATUS_ACCESS_VIOLATION at eip=610B5FF2
eax=0D89466C ebx=006A02F0 ecx=61149C88 edx=0D89466C esi=61149C88 edi=006C05C8
ebp=0022CAC8 esp=0022CAB0 program=c:\Documents and Settings\sfilipek\test\FileTest.exe, pid 4344, thread main
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame     Function  Args
0022CAC8  610B5FF2  (006A02F0, 00000000, 0022CAE8, 006A02F0)
0022CAE8  610B8B0D  (006A0298, FFFFFFFF, 0022CC98, 006B0508)
0022CC08  610B1E4B  (0022CC20, 0022CC94, 0022CCE8, 610935A8)
0022CC18  610779F8  (006A0298, 0022CC94, 00401150, 0022CCA0)
0022CCE8  610935A8  (00000001, 6116B798, 006A0090, 0022CC70)
0022CD98  610060D8  (00000000, 0022CDD0, 61005450, 0022CDD0)
61005450  61004416  (0000009C, A02404C7, E8611021, FFFFFF48)
     34 [main] FileTest 4344 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)

Nothing was written to stderr in the end... just the segfault.

Any advice, workaround, etc. would be extremely helpful.

Regards,
Stefan Filipek

uname -a: CYGWIN_NT-5.1 [computer name] 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

Attachment: cygcheck.out
Description: cygcheck.out

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]