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]

Re: 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64)


On Jun 21 06:04, RG wrote:
> Hello,
> 
> I have encountered unexpected behavior when using flock.
> It will block forever, but to my understanding it should not.
> I've attached a simple program that would trigger the behavior.
> 
> Regards,
> Alex

> #include <stdio.h>
> #include <unistd.h>
> #include <sys/file.h>
> 
> FILE *fp;
> 
> void main() {
>     unlink("a"); unlink("b");
> 
>     fp = fopen("a", "wb"); // file a
> 
>     printf("lock a\n");
>     flock(fileno(fp), LOCK_EX);
> 
>     fwrite("TEST", sizeof(char), 4, fp);
> 
>     printf("unlock a\n");
>     flock(fileno(fp), LOCK_UN);
>     fclose(fp);
> 
>     fp = fopen("b", "wb");  // file b
> 
>     printf("lock b\n");
>     flock(fileno(fp), LOCK_EX);  // hang
> 
>     fwrite("TEST", sizeof(char), 4, fp);
> 
>     printf("unlock b\n");
>     flock(fileno(fp), LOCK_UN);
>     fclose(fp);
> }

Thank you for the report and especially the testcase!

This is one of those long-standing problems where you shake your
head violently and wonder why nobody noticed this earlier.  The
problem only occurs for newly created files, and only if they get
created in the same parent dir.

What happoens is that the inode number for the files (which is crucial
for flock) is yet incorrectly the inode number of the parent directory
after fopen. However, due to the way fwrite works, its call results in
fetching the correct inode number of the file.  The subsequent flock
fails to unlock because searching the file by inode number suddenly
fails.

I uploaded new developer snapshots on https://cygwin.com/snapshots/,

I also created and just announced a new test release 2.5.2-0.2 which
contains a fix for this problem and can be easily installed via setup.

Please give any of them a try ASAP.


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

Attachment: signature.asc
Description: PGP signature


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