public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: 2.5.1: flock blocks when it shouldn't (NT-10.0, x86_64)
Date: Tue, 21 Jun 2016 14:03:00 -0000	[thread overview]
Message-ID: <20160621122314.GA12441@calimero.vinschen.de> (raw)
In-Reply-To: <CAMpcqEHb+UarN8WhMdONDOW3iJa5Zi+c49_RnYq8zXHNYWCvKQ@mail.gmail.com>

[-- 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 --]

      reply	other threads:[~2016-06-21 12:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-21  9:04 RG
2016-06-21 14:03 ` Corinna Vinschen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160621122314.GA12441@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).