Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2007-12-15
05:29 • Ticket [1593707fff] win32 error during simultaneous file open for read statu... artifact: be1ff782f5 user: georgeps
2007-07-19
04:38 • Ticket [1593707fff]: 6 changes artifact: 587c218cc0 user: melbardis
2007-03-21
20:12 • Ticket [1593707fff]: 1 change artifact: ce4a5520f5 user: melbardis
2006-11-10
02:35 • Add attachment tclWinChan.c to ticket [1593707fff] artifact: 64effa4d58 user: melbardis
02:35 • Ticket [1593707fff] win32 error during simultaneous file open for read statu... artifact: bd74cb11de user: melbardis
02:35 • Ticket [1593707fff]: 4 changes artifact: 9211d5dbd1 user: melbardis
02:35 • Add attachment ct.tcl to ticket [1593707fff] artifact: 8b16f23bc7 user: melbardis
2006-11-09
19:35 • New ticket [1593707fff] win32 error during simultaneous file open for read. artifact: 2a7476de8a user: melbardis

win32 error during simultaneous file open for read

Open1593707fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 19 years ago
Descriptionplain2006-11-09 19:35:00
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
melbardisplain2006-11-10 02:35:00
File Added - 202137: ct.tcl
melbardisplain2006-11-10 02:35:53
File Added - 202139: tclWinChan.c
melbardis Ticket [1593707fff]: 1 change [History diff]2007-03-21 20:12:39
melbardisplain2007-07-19 04:38:20
Logged In: YES 
user_id=788816
Originator: YES

no response when filed under the previous category!
georgepsplain2007-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...
Log in (anonymous is fine) to add a comment.

Attachments: