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

Corinna Vinschen corinna-cygwin@cygwin.com
Tue Jun 21 14:03:00 GMT 2016


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20160621/49b4815a/attachment.sig>


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /