public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64)
@ 2016-06-21  9:04 RG
  2016-06-21 14:03 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: RG @ 2016-06-21  9:04 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 206 bytes --]

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

[-- Attachment #2: flock_hang.c --]
[-- Type: text/x-csrc, Size: 553 bytes --]

#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);
}

[-- Attachment #3: Type: text/plain, Size: 218 bytes --]

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64)
  2016-06-21  9:04 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64) RG
@ 2016-06-21 14:03 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2016-06-21 14:03 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1988 bytes --]

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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-06-21 12:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-21  9:04 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64) RG
2016-06-21 14:03 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).