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

[ZOOKEEPER-2338] - set SOCK_CLOEXEC on socket if defined #410

Closed
wants to merge 1 commit into from
Closed

[ZOOKEEPER-2338] - set SOCK_CLOEXEC on socket if defined #410

wants to merge 1 commit into from

Conversation

fr0stbyte
Copy link
Contributor

No description provided.

@afine
Copy link
Contributor

afine commented Oct 27, 2017

Hi @fr0stbyte

I see in the JIRA that this ticked arises from an issue related to running ZooKeeper Mesos. Would you be able to explain exactly why this is needed?

@fr0stbyte
Copy link
Contributor Author

@afine Thanks for looking at the PR
Here is the bug tracked on Mesos side : https://issues.apache.org/jira/browse/MESOS-4065

@afine
Copy link
Contributor

afine commented Nov 2, 2017

@fr0stbyte Thanks for linking to the Mesos bug. Unfortunately, I am not familiar with the way Mesos handles processes and I think it is possible that other members of the community share my ignorance.

Would you mind describing the conditions that are necessary to reproduce this bug?

@anmolnar
Copy link
Contributor

anmolnar commented Nov 4, 2017

@fr0stbyte Reading some Stackoverflow about the flag (https://stackoverflow.com/questions/22304631/what-is-the-purpose-to-set-sock-cloexec-flag-with-accept4-same-as-o-cloexec), the change looks reasonable to me. However it would be beneficial to shed some light on how it's related to your scenario.

@phunt
Copy link
Contributor

phunt commented Nov 15, 2017

This looks like a non-backward compatible change to me. That said I'm not sure how many clients would rely on this behavior. thoughts?

@fr0stbyte
Copy link
Contributor Author

@phunt I'd have no problem making in backward compatible, but I am not sure how. Can you offer some guidance here ?
@anmolnar In the description of MESOS-4065 you can see 2 processes that share the same open file descriptor. In Linux ( Unix in general ) upon forking, the child inherits all the open files of the parent, unless the O_CLOEXEC option has been set on the file descriptor. My change uses the SOCK_CLOEXEC which is an atomic set of that flag. The alternative is to open it and then use fcntl to set the flag, but that leaves space for a race as a process could fork before.
Since the child process does not need / makes use of the open file descriptor it is proper to close it before exec'ing. I wouldn't say it's a functional bug, but rather good housekeeping ( afaict ).
@afine I think in order to reproduce it, you need a process that uses the C client, then that forks and execs something else. To verify, look at the open file descriptors for the 2 processes and verify that the open socket to zookeeper is not available in the child.

@phunt
Copy link
Contributor

phunt commented Nov 17, 2017

I am not sure how

Yea, same here I'm afraid.

In thinking about it a bit perhaps what we could do is one of the following two option (might be more):

  1. add it with some extra tooling in autoconf (and probably now cmake since it was recently introduced). Basically: add a new flag that the user (person compiling the library) could use to enable/disable SOCK_CLOEXEC when running ./configure.

In 3.5 the default behavior would be to not use SOCK_CLOEXEC regardless whether it's supported or not (user can override default with the option). In 3.6 and later we'd make SOCK_CLOEXEC the default and call it out as a potential issue as part of the release notes (disable with the option in this case).

  1. it seems like we could do similar with a runtime property that the user could set in code prior to running zookeeper_init that would be global in nature. e.g. zoo_deterministic_conn_order

Again the defaults would be as specified in 1) above.

Thoughts?

@fr0stbyte
Copy link
Contributor Author

@phunt both options seem fine to me. I think would prefer the first one, that way if the library remains ABI compatible, the clients won't need to recompile.

@fr0stbyte
Copy link
Contributor Author

@phunt Let me know if the changes are inline with what you've had in mind. Once we clear that, I will add the changes for 3.6

@fr0stbyte
Copy link
Contributor Author

@phunt @anmolnar any suggestion on changes or does this look good ?

@anmolnar
Copy link
Contributor

anmolnar commented Dec 6, 2017

@fr0stbyte According to my tiny C knowledge, it looks good to me.

Are u going to create a separate PR for trunk?

@phunt
Copy link
Contributor

phunt commented Dec 6, 2017

I tested it with cmake and autotools based generation and the command lines work properly with agreed upon defaults. LGTM. +1.

asfgit pushed a commit that referenced this pull request Dec 6, 2017
Author: Radu Brumariu <radu@groupon.com>

Reviewers: phunt@apache.org

Closes #410 from fr0stbyte/ZOOKEEPER-2338

Change-Id: I7b060a2dc473b34aecc0cfac71d39720369bd635
@asfgit asfgit closed this in 665038e Dec 6, 2017
@phunt
Copy link
Contributor

phunt commented Dec 6, 2017

Committed to branch-3.5 and master. I tried applying to branch-3.4 but it wouldn't apply cleanly. Please submit another pull request for 3.4 and I'll review/commit that as well. Thanks @fr0stbyte !

lvfangmin pushed a commit to lvfangmin/zookeeper that referenced this pull request Jun 17, 2018
Author: Radu Brumariu <radu@groupon.com>

Reviewers: phunt@apache.org

Closes apache#410 from fr0stbyte/ZOOKEEPER-2338

Change-Id: I7b060a2dc473b34aecc0cfac71d39720369bd635
(cherry picked from commit 508293d)
Signed-off-by: Patrick Hunt <phunt@apache.org>
RokLenarcic pushed a commit to RokLenarcic/zookeeper that referenced this pull request Sep 3, 2022
Author: Radu Brumariu <radu@groupon.com>

Reviewers: phunt@apache.org

Closes apache#410 from fr0stbyte/ZOOKEEPER-2338

Change-Id: I7b060a2dc473b34aecc0cfac71d39720369bd635
(cherry picked from commit 508293d)
Signed-off-by: Patrick Hunt <phunt@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants