Race condition with custom NSURLProtocol ignores HTTP 3xx redirects

Originator:andrew_morrow
Number:rdar://39391319 Date Originated:12 April 2018
Status:Open Resolved:
Product:macOS + SDK Product Version:10.13.4
Classification:Bug Reproducible:Sometimes
 
Area:
Foundation

Summary:
When using a custom NSURLProtocol subclass to intercept HTTP requests, NSURLSession sometimes ignores 302 redirects and instead returns the 302 response or no response whatsoever. No error is returned.

If the NSURLSessionTaskDelegate artificially introduces a delay in URLSession: task: willPerformHTTPRedirection: ..., the bug is no longer reproducible.

Steps to Reproduce:
Run the unit tests in the attached sample project. On average, the redirection without delay fails 1-2 times per test run. The redirection with a delay introduced by usleep(500) has not yet failed.

If you do not observe any test failures on the first run, please try once or twice more. If you observe the delayed tests failing, please try increasing the usleep parameter.

Expected Results:
NSURLSession should always follow 302 redirects and return the contents of the final URL, or else it should return an error.

Actual Results:
Randomly, the 302 is not followed and instead the body of the original response is returned. No error is returned.

Version/Build:
macOS 10.13.4 (17E199)

The issue has been known and reproducible since August 2016 as seen in this OHHTTPStubs bug report:
https://github.com/AliSoftware/OHHTTPStubs/issues/230

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!