Skip to content

Commit bf62c64

Browse files
author
Ignacio Bonafonte
committed
Fix network instrumentation was not reporting concurrent network requests
The task identifier was being set out of the callback implementation so it was being reused, if the session tried to send concurrent requests, only the first one would be reported.
1 parent 1a43c39 commit bf62c64

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public class URLSessionInstrumentation {
114114
return
115115
}
116116
var originalIMP: IMP?
117-
let sessionTaskId = UUID().uuidString
118117

119118
let block: @convention(block) (URLSession, AnyObject) -> URLSessionTask = { session, argument in
120119
if let url = argument as? URL {
@@ -130,6 +129,7 @@ public class URLSessionInstrumentation {
130129

131130
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, Any) -> URLSessionDataTask).self)
132131
var task: URLSessionTask
132+
let sessionTaskId = UUID().uuidString
133133

134134
if let request = argument as? URLRequest, objc_getAssociatedObject(argument, &idKey) == nil {
135135
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: sessionTaskId, instrumentation: self, shouldInjectHeaders: true)
@@ -161,9 +161,9 @@ public class URLSessionInstrumentation {
161161
return
162162
}
163163
var originalIMP: IMP?
164-
let sessionTaskId = UUID().uuidString
165164

166165
let block: @convention(block) (URLSession, URLRequest, AnyObject) -> URLSessionTask = { session, request, argument in
166+
let sessionTaskId = UUID().uuidString
167167
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, URLRequest, AnyObject) -> URLSessionDataTask).self)
168168
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: sessionTaskId, instrumentation: self, shouldInjectHeaders: true)
169169
let task = castedIMP(session, selector, instrumentedRequest ?? request, argument)
@@ -190,7 +190,6 @@ public class URLSessionInstrumentation {
190190
return
191191
}
192192
var originalIMP: IMP?
193-
let sessionTaskId = UUID().uuidString
194193

195194
let block: @convention(block) (URLSession, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionTask = { session, argument, completion in
196195

@@ -216,6 +215,7 @@ public class URLSessionInstrumentation {
216215

217216
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, Any, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionDataTask).self)
218217
var task: URLSessionTask!
218+
let sessionTaskId = UUID().uuidString
219219

220220
var completionBlock = completion
221221

@@ -275,13 +275,14 @@ public class URLSessionInstrumentation {
275275
return
276276
}
277277
var originalIMP: IMP?
278-
let sessionTaskId = UUID().uuidString
279278

280279
let block: @convention(block) (URLSession, URLRequest, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionTask = { session, request, argument, completion in
281280

282281
let castedIMP = unsafeBitCast(originalIMP, to: (@convention(c) (URLSession, Selector, URLRequest, AnyObject, ((Any?, URLResponse?, Error?) -> Void)?) -> URLSessionDataTask).self)
283282

284283
var task: URLSessionTask!
284+
let sessionTaskId = UUID().uuidString
285+
285286
var completionBlock = completion
286287
if objc_getAssociatedObject(argument, &idKey) == nil {
287288
let completionWrapper: (Any?, URLResponse?, Error?) -> Void = { object, response, error in

0 commit comments

Comments
 (0)