Tcl Source Code

View Ticket
Login
Ticket UUID: 1593707
Title: win32 error during simultaneous file open for read
Type: Bug Version: None
Submitter: melbardis Created on: 2006-11-09 19:35:00
Subsystem: 49. Threading Assigned To: vincentdarley
Priority: 8 Severity:
Status: Open Last Modified: 2007-12-15 05:29:30
Resolution: None Closed By:
    Closed on:
Description:
If threads in a multi threaded application (or simply 
multiple copies of a tclsh) try to open the same file 
for reading, there appears to be a window of 
opportunity for windows to return an error, typically 
a ERROR_SHARING_VIOLATION error.

Additionally (in another of my applications non-tcl) 
i found that is a thread closes a file and another 
thread trys to delete the file, the same window can 
apply.

I have modfied win/tclWinChan.c (8.54a, though same 
code seems to exist everywhere...) as follows:

    /*
     * Now we get to create the file.
     */
    {
        //EPM - windows can screwup when opening same 
file at same time
        int retry = 5;
        int err;
        while (retry--) {
            handle = (*tclWinProcs->createFileProc)
(nativeName, accessMode,
                                                    
shareMode, NULL, createMode, flags, (HANDLE) NULL);
            if (handle != INVALID_HANDLE_VALUE) {
                break;
            }
            err = GetLastError();
            if (err == ERROR_FILE_NOT_FOUND) {
                    // File removed by external 
circumstances.
                    break;
            }
            Sleep(100);
            //printf("retry... %d\n", retry);
        }
    }


if the retry message is uncommented, then the retry 
was output occasionally when running the test code, 
with a retry value of 4

note: my machine is a dual-core pentium 2.8 gig
User Comments: georgeps added on 2007-12-15 05:29:30:
Logged In: YES 
user_id=585068
Originator: NO

After reading MSDN I'm not sure this is a bug that is fixable in Tcl, without a hack that really wouldn't be appropriate.

To quote from MSDN: "When a process attempts to open a file that has already been opened in sharing mode, the system compares the requested access and sharing modes to those specified when the file was opened. If you specify an access mode that conflicts with the sharing mode specified in the previous open call, CreateFile fails with a sharing violation (ERROR_SHARING_VIOLATION)."

I think this means you just can't do some things in Windows that you want to do, unless you remove the FILE_SHARE_* flags from the CreateFile call.

However, I just noticed that NativeStat() in the win tree uses "FILE_SHARE_READ | FILE_SHARE_WRITE" (used to detect permissions it seems) with CreateFile.  There could be a race between the stat in one thread, and an open in another.  So even though you only want one handle open, the stat is causing the open to fail in some cases.  This isn't good...

melbardis added on 2007-07-19 04:38:20:
Logged In: YES 
user_id=788816
Originator: YES

no response when filed under the previous category!

melbardis added on 2006-11-10 02:35:53:

File Added - 202139: tclWinChan.c

melbardis added on 2006-11-10 02:35:00:

File Added - 202137: ct.tcl

Attachments: