Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add file-based lock for old iptables versions #12

Merged
merged 1 commit into from
Nov 24, 2015

Conversation

jonboulle
Copy link
Contributor

iptables <1.4.20 does not support blocking (the --wait option) to
prevent concurrent invocations from interrupting each other. To work
around this, this patch adds a file-based lock (flock), using the same
file that xtables now uses 1.

Note that this follows a similar behaviour to xtables in that it's
best-effort only: if the lock can't be acquired for some reason,
operations proceed anyway.

h/t to @janeczku and @adfernandes for original fix suggestions


// Unlock closes the lock which implicitly unlocks it as well
func (l *fileLock) Unlock() error {
l.mu.Lock()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, this isn't right

iptables <1.4.20 does not support blocking (the `--wait` option) to
prevent concurrent invocations from interrupting each other. To work
around this, this patch adds a file-based lock (flock), using the same
file that xtables now uses [1] (`/run/xtables.lock`).

Note that this follows a similar behaviour to xtables in that it's
best-effort only: if the lock can't be acquired for some reason,
operations proceed anyway.

h/t to @janeczku and @adfernandes for original fix suggestions

[1]: http://git.netfilter.org/iptables/commit/?id=aa562a660d1555b13cffbac1e744033e91f82707
@adfernandes
Copy link

👍 Merci beaucoup monsieur!


type fileLock struct {
// mu is used to protect against concurrent invocations from within this process
mu sync.Mutex
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me what exactly this is serializing access to. The only thing here is the fd, and the fd doesn't get written to at any point after instantiating the lock, so we shouldn't need to serialize access to it should we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed OOB: the idea is that the file lock is used for process concurrency and the mutex lock is for thread (goroutine) concurrency - since the fileLock is file-descriptor based and hence repeated locks would just succeed anyway.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed OOB, it's fine.

@vcaputo
Copy link

vcaputo commented Nov 24, 2015

I don't like the copy-and-paste "--wait" args append stuff, but the defer in there interferes with using a function, golang... sigh.

lgtm

jonboulle added a commit that referenced this pull request Nov 24, 2015
*: add file-based lock for old iptables versions
@jonboulle jonboulle merged commit 6c5555f into coreos:master Nov 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants