Skip to content

Commit 9869f41

Browse files
committed
Only allow one delegate method to be called at once.
Fixes dart-lang#785
1 parent 4fc3d58 commit 9869f41

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Make the number of simulateous connections allowed to the same host
44
configurable.
5+
* Fixes
6+
[cupertino_http: Failure calling Dart_PostCObject_DL](https://github.com/dart-lang/http/issues/785).
57

68
## 0.0.5
79

pkgs/cupertino_http/lib/cupertino_http.dart

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -917,28 +917,38 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
917917
///
918918
/// See [sessionWithConfiguration:delegate:delegateQueue:](https://developer.apple.com/documentation/foundation/nsurlsession/1411597-sessionwithconfiguration)
919919
factory URLSession.sessionWithConfiguration(URLSessionConfiguration config,
920-
{URLRequest? Function(URLSession session, URLSessionTask task,
921-
HTTPURLResponse response, URLRequest newRequest)?
922-
onRedirect,
923-
URLSessionResponseDisposition Function(URLSession session,
924-
URLSessionTask task, URLResponse response)?
925-
onResponse,
926-
void Function(URLSession session, URLSessionTask task, Data error)?
927-
onData,
928-
void Function(
929-
URLSession session, URLSessionDownloadTask task, Uri uri)?
930-
onFinishedDownloading,
931-
void Function(URLSession session, URLSessionTask task, Error? error)?
932-
onComplete}) =>
933-
URLSession._(
934-
ncb.NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
935-
linkedLibs, config._nsObject, _delegate, null),
936-
onRedirect: onRedirect,
937-
onResponse: onResponse,
938-
onData: onData,
939-
onFinishedDownloading: onFinishedDownloading,
940-
onComplete: onComplete);
941-
920+
{URLRequest? Function(URLSession session, URLSessionTask task,
921+
HTTPURLResponse response, URLRequest newRequest)?
922+
onRedirect,
923+
URLSessionResponseDisposition Function(
924+
URLSession session, URLSessionTask task, URLResponse response)?
925+
onResponse,
926+
void Function(URLSession session, URLSessionTask task, Data error)?
927+
onData,
928+
void Function(URLSession session, URLSessionDownloadTask task, Uri uri)?
929+
onFinishedDownloading,
930+
void Function(URLSession session, URLSessionTask task, Error? error)?
931+
onComplete}) {
932+
// Avoid the complexity of simultaenous or out-of-order delegate callbacks
933+
// by only allowing callbacks to execute sequentially.
934+
// See https://developer.apple.com/forums/thread/47252
935+
// NOTE: this is likely to reduce throughput when there are multiple
936+
// requests in flight because each call to a delegate waits on a lock
937+
// that is unlocked by Dart code.
938+
final queue = ncb.NSOperationQueue.new1(linkedLibs)
939+
..maxConcurrentOperationCount = 1
940+
..name =
941+
'cupertino_http.NSURLSessionDelegateQueue'.toNSString(linkedLibs);
942+
943+
return URLSession._(
944+
ncb.NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
945+
linkedLibs, config._nsObject, _delegate, queue),
946+
onRedirect: onRedirect,
947+
onResponse: onResponse,
948+
onData: onData,
949+
onFinishedDownloading: onFinishedDownloading,
950+
onComplete: onComplete);
951+
}
942952
// A **copy** of the configuration for this sesion.
943953
//
944954
// See [NSURLSession.configuration](https://developer.apple.com/documentation/foundation/nsurlsession/1411477-configuration)

0 commit comments

Comments
 (0)