Skip to content

Commit ca7e686

Browse files
authored
Merge pull request #12439 from MaxHearnden/cloexec
Set FD_CLOEXEC on sockets created by curl
2 parents fda585f + 12d2527 commit ca7e686

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

doc/manual/rl-next/curl-cloexec.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
synopsis: Set FD_CLOEXEC on sockets created by curl
3+
issues: []
4+
prs: [12439]
5+
---
6+
7+
8+
Curl creates sockets without setting FD_CLOEXEC/SOCK_CLOEXEC, this can cause connections to remain open forever when using commands like `nix shell`
9+
10+
This change sets the FD_CLOEXEC flag using a CURLOPT_SOCKOPTFUNCTION callback.

src/libstore/filetransfer.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ struct curlFileTransfer : public FileTransfer
300300
return ((TransferItem *) userp)->readCallback(buffer, size, nitems);
301301
}
302302

303+
#if !defined(_WIN32) && LIBCURL_VERSION_NUM >= 0x071000
304+
static int cloexec_callback(void *, curl_socket_t curlfd, curlsocktype purpose) {
305+
unix::closeOnExec(curlfd);
306+
vomit("cloexec set for fd %i", curlfd);
307+
return CURL_SOCKOPT_OK;
308+
}
309+
#endif
310+
303311
void init()
304312
{
305313
if (!req) req = curl_easy_init();
@@ -359,6 +367,10 @@ struct curlFileTransfer : public FileTransfer
359367
curl_easy_setopt(req, CURLOPT_SSL_VERIFYHOST, 0);
360368
}
361369

370+
#if !defined(_WIN32) && LIBCURL_VERSION_NUM >= 0x071000
371+
curl_easy_setopt(req, CURLOPT_SOCKOPTFUNCTION, cloexec_callback);
372+
#endif
373+
362374
curl_easy_setopt(req, CURLOPT_CONNECTTIMEOUT, fileTransferSettings.connectTimeout.get());
363375

364376
curl_easy_setopt(req, CURLOPT_LOW_SPEED_LIMIT, 1L);

0 commit comments

Comments
 (0)