Skip to content

Conversation

@clintonpi
Copy link

Motivation:

URLSession elusively crashes due to a race condition in OpenSSL <1.1.0 when calling curl_multi_cleanup concurrently in different threads.

Partially resolves #3813

Modifications:

  • Adjust _MultiHandle.deinit to asynchronously queue curl_multi_cleanup calls in _MultiHandle.queue.

Result:

Saves OpenSSL from cleaning up concurrently in different threads, avoiding the crash.

Testing:

Passing existing tests is sufficient. However, without this patch, the code snippet below crashes about once in 20 runs on Amazon Linux 2

curl 8.3.0 (aarch64-koji-linux-gnu) libcurl/8.3.0 OpenSSL/1.0.2k-fips zlib/1.2.7 libidn2/2.3.0 libpsl/0.21.5 (+libidn2/2.3.0) libssh2/1.4.3 nghttp2/1.41.0 OpenLDAP/2.4.44
Release-Date: 2023-09-13

DispatchQueue.concurrentPerform(iterations: 100) { _ in
    let session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
    DispatchQueue.concurrentPerform(iterations: 100) { _ in
        _ = session
    }
}

Motivation:

`URLSession` elusively crashes due to a race condition in OpenSSL <1.1.0 when calling `curl_multi_cleanup` concurrently in different threads.

_Partially resolves #3813_

Modifications:

- Adjust `_MultiHandle.deinit` to asynchronously queue `curl_multi_cleanup` calls in `_MultiHandle.queue`.

Result:

Saves OpenSSL from cleaning up concurrently in different threads, avoiding the crash.

Testing:

Passing existing tests is sufficient. However, without this patch, the code snippet below crashes about once in 20 runs on Amazon Linux 2
> _curl 8.3.0 (aarch64-koji-linux-gnu) libcurl/8.3.0 OpenSSL/1.0.2k-fips zlib/1.2.7 libidn2/2.3.0 libpsl/0.21.5 (+libidn2/2.3.0) libssh2/1.4.3 nghttp2/1.41.0 OpenLDAP/2.4.44_
> _Release-Date: 2023-09-13_

```
DispatchQueue.concurrentPerform(iterations: 100) { _ in
    let session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
    DispatchQueue.concurrentPerform(iterations: 100) { _ in
        _ = session
    }
}
```
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.

[SR-5761] corelibs-foundation: fix concurrency/fd ownership in MultiHandle.swift

1 participant