Skip to contentSkip to conversation

Does anyone know when FUTEX_WAIT can fail with EAGAIN?

DISCUSSION Steven Stewart-Gallus 3 participants 3 messages Jul 29, 2014 – Aug 17, 2014 ABI/API

lore
Share & tools
lore
#0 lore raw

Hello,

I'm trying to debug a hangup where my program loops with FUTEX_WAIT (actually FUTEX_WAIT_PRIVATE but same thing) endlessly erring out with EAGAIN. I would like to know if anyone on the mailing list knows when FUTEX_WAIT can fail with EAGAIN.

Some info for people who are interested in what I'm working on.

I'm working on sand boxing my program at gitorious.org/linted with namespaces and chroot.

The error only appeared after changing unshare then fork code into a direct clone call.

Quoted 2 lines

| uname -a
Linux alonzo 3.13.0-24-generic #1trisquel1 SMP Tue May 13 15:17:50 UTC 2014

x86_64 x86_64 x86_64 GNU/Linux

Thank you,
Steven Stewart-Gallus

#1 lore raw
Quoted 4 lines · Steven Stewart-Gallus

I'm trying to debug a hangup where my program loops with FUTEX_WAIT (actually FUTEX_WAIT_PRIVATE but same thing) endlessly erring out with EAGAIN. I would like to know if anyone on the mailing list knows when FUTEX_WAIT can fail with EAGAIN.

FUTEX_WAIT fails with EAGAIN if the value of *uaddr no longer matches the expected val.
---
#include <linux/futex.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>

int main(void) {
	int op = FUTEX_WAIT;
	int exp = 1;
	int *addr = &exp;
	int val = 1; /* XXX change this to anything else to get EAGAIN */
	int rc = syscall(SYS_futex, addr, op, val, 0, 0, 0);

	if (rc < 0 && errno == EAGAIN)
		write(1, "EAGAIN\n", 7);

	return 0;
}

---
I just encountered this myself yesterday while implementing futex-based locks/condvars for Ruby:
https://bugs.ruby-lang.org/issues/10009#change-48372

#2 lore raw
In reply to Steven Stewart-Gallus
Quoted 6 lines · Steven Stewart-Gallus

Hello,

I'm trying to debug a hangup where my program loops with FUTEX_WAIT (actually FUTEX_WAIT_PRIVATE but same thing) endlessly erring out with EAGAIN. I would like to know if anyone on the mailing list knows when FUTEX_WAIT can fail with EAGAIN.

You're probably running into the EWOULDBLOCK return -- which on Linux is just another name for EAGAIN. So if you didn't know this, and were reading the code related to FUTEX_WAIT, you'd get mislead by this.

The check is there to avoid userspace misusing futexes and screwing up applications. When the futex_wait() call is issued, a lot can happen in the kernel before the task is blocked (key hashing, reference counting, acquire internal spinlock, etc.). At any point while preparing to block, another userspace thread could have changed the futex value, and thus the condition for which futex_wait() call was issued in the first place might no longer be true.

Thanks,
Davidlohr