Bug 155256 - iOS-sim debug: WebCoreNSURLSessionTest.BasicOperation and WebCoreNSURLSessionTest.InvalidateEmpty asserting
Summary: iOS-sim debug: WebCoreNSURLSessionTest.BasicOperation and WebCoreNSURLSession...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords:
Depends on:
Blocks: 155035
  Show dependency treegraph
 
Reported: 2016-03-09 13:38 PST by Ryan Haddad
Modified: 2016-03-11 12:26 PST (History)
9 users (show)

See Also:


Attachments
Patch (2.65 KB, patch)
2016-03-10 20:05 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (3.13 KB, patch)
2016-03-11 09:55 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (3.11 KB, patch)
2016-03-11 09:57 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (14.26 KB, patch)
2016-03-11 11:01 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (14.32 KB, patch)
2016-03-11 11:35 PST, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Haddad 2016-03-09 13:38:09 PST
iOS-sim debug: WebCoreNSURLSessionTest.BasicOperation and WebCoreNSURLSessionTest.InvalidateEmpty asserting

Tests that failed:
  WebCoreNSURLSessionTest.BasicOperation
  WebCoreNSURLSessionTest.InvalidateEmpty

ASSERTION FAILED: s_mainRunLoop
/Volumes/Data/slave/ios-simulator-9-debug/build/Source/WTF/wtf/RunLoop.cpp(72) : static bool WTF::RunLoop::isMain()
1   0x10c6dee40 WTFCrash
2   0x10c72bd52 WTF::RunLoop::isMain()
3   0x1123f571d WebCore::applicationBundleIdentifier()
4   0x1123f5901 WebCore::applicationBundleIsEqualTo(WTF::String const&)
5   0x1123f6355 WebCore::IOSApplication::isTheEconomistOnIphone()
6   0x11c8e8820 +[WebView(WebPrivate) enableWebThread]
7   0x11c8d91f6 WebKitInitialize
8   0x11b30cf61 ___UIApplicationLoadWebKit_block_invoke
9   0x119dcc4a7 _dispatch_client_callout
10  0x119db9c7b dispatch_once_f
11  0x118f54bff _class_initialize
12  0x118f5ad23 lookUpImpOrForward
13  0x118f698bb objc_msgSend
14  0x10b21b608 TestWebKitAPI::WebKit1_AudioSessionCategoryIOS_Test::TestBody()
15  0x10b441a8a testing::Test::Run()
16  0x10b44236d testing::internal::TestInfoImpl::Run()
17  0x10b442f0d testing::TestCase::Run()
18  0x10b448e92 testing::internal::UnitTestImpl::RunAllTests()
19  0x10b448b09 testing::UnitTest::Run()
20  0x10b35f6ee TestWebKitAPI::TestsController::run(int, char**)
21  0x10b3d6065 main
22  0x119dfb92d start

<https://build.webkit.org/builders/Apple%20iOS%209%20Simulator%20Debug%20WK2%20(Tests)/builds/89/steps/run-api-tests/logs/stdio>
Comment 1 Alexey Proskuryakov 2016-03-10 17:59:57 PST
static String& applicationBundleIdentifier()
{
    ASSERT(RunLoop::isMain());

    static NeverDestroyed<String> identifier([[NSBundle mainBundle] bundleIdentifier]);

    return identifier;
}

Main run loop is never set up by iOS WebKit1, and it doesn't make a lot of sense there.

I think that it is a bug in application check code that it tries to use NSBundle from the web thread (the test crashes in main thread, but I think that this code would run the on web thread normally).
Comment 2 Chris Dumez 2016-03-10 18:36:30 PST
Oh, I added this assertion recently and did not realize this would be an issue on WK1.
Comment 3 Chris Dumez 2016-03-10 18:37:58 PST
Very likely a regression from <http://trac.webkit.org/changeset/197628>
Comment 4 Chris Dumez 2016-03-10 18:59:04 PST
(In reply to comment #3)
> Very likely a regression from <http://trac.webkit.org/changeset/197628>

So before r197628, we were always using [[NSBundle mainBundle] bundleIdentifier] directly on iOS (no matter the thread). So I propose the restore this behavior as follows:
diff --git a/Source/WebCore/platform/RuntimeApplicationChecks.mm b/Source/WebCore/platform/RuntimeApplicationChecks.mm
index b76bb2e..b2583bd 100644
--- a/Source/WebCore/platform/RuntimeApplicationChecks.mm
+++ b/Source/WebCore/platform/RuntimeApplicationChecks.mm
@@ -28,7 +28,7 @@
 
 #import <Foundation/NSBundle.h>
 #import <wtf/NeverDestroyed.h>
-#import <wtf/RunLoop.h>
+#import <wtf/MainThread.h>
 #import <wtf/text/WTFString.h>
 
 namespace WebCore {
@@ -39,7 +39,7 @@ namespace WebCore {
 // the WK2 UIProcess.
 static String& applicationBundleIdentifier()
 {
-    ASSERT(RunLoop::isMain());
+    ASSERT(isMainThread());
 
     static NeverDestroyed<String> identifier([[NSBundle mainBundle] bundleIdentifier]);
 
@@ -53,6 +53,10 @@ void setApplicationBundleIdentifier(const String& bundleIdentifier)
 
 static bool applicationBundleIsEqualTo(const String& bundleIdentifierString)
 {
+#if USE(WEB_THREAD)
+    if (isWebThread())
+        return [[NSBundle mainBundle] bundleIdentifier] == bundleIdentifierString;
+#endif
     return applicationBundleIdentifier() == bundleIdentifierString;
 }

I will test before uploading but let me know if you have any thoughts.

If we are in the WebThread, then we are using WK1 and therefore we don't care about the applicationBundleIdentifier(), main bundle identifier does the right thing.

For other cases (WK2 or WK1 main thread), we keep using the static applicationBundleIdentifier.
Comment 5 Alexey Proskuryakov 2016-03-10 19:44:57 PST
*** Bug 155275 has been marked as a duplicate of this bug. ***
Comment 6 Chris Dumez 2016-03-10 20:05:01 PST
Created attachment 273674 [details]
Patch
Comment 7 Alexey Proskuryakov 2016-03-10 22:25:15 PST
Comment on attachment 273674 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=273674&action=review

> Source/WebCore/platform/RuntimeApplicationChecks.mm:42
> -    ASSERT(RunLoop::isMain());
> +    ASSERT(isMainThread());

I'm not sure if this change is right. This assertion use to say that we are only using NSBundle for the main thread, which is the right thing to require given that NSBundle is not thread safe.

isMainThread() is a lot more complicated beast, and I don't really see how it verifies anything useful.
Comment 8 Alexey Proskuryakov 2016-03-10 22:28:19 PST
And if we are concerned about thread safety of WebCore code here (as opposed to NSBundle), then this function can use dispatch_once. I actually don't know if we enable thread-safe statics.
Comment 9 Alexey Proskuryakov 2016-03-11 09:07:05 PST
We compile with -fno-threadsafe-statics. Also, as Chris pointed out in person, String is not thread safe anyway.
Comment 10 Chris Dumez 2016-03-11 09:18:34 PST
Comment on attachment 273674 [details]
Patch

After discussing this with Alexey, I have decided to take another stab at this.
Comment 11 Chris Dumez 2016-03-11 09:55:31 PST
Created attachment 273741 [details]
WIP Patch
Comment 12 Chris Dumez 2016-03-11 09:57:44 PST
Created attachment 273742 [details]
WIP Patch
Comment 13 Chris Dumez 2016-03-11 10:35:36 PST
I have a better / simpler idea. I will upload another patch shortly.
Comment 14 Chris Dumez 2016-03-11 11:01:44 PST
Created attachment 273747 [details]
Patch
Comment 15 Alexey Proskuryakov 2016-03-11 11:10:24 PST
Comment on attachment 273747 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=273747&action=review

> Source/WebCore/platform/RuntimeApplicationChecks.mm:37
> +static bool wasApplicationBundleIdentifierOverrideQueried = false;

Nitpick: I'd name this applicationBundleIdentifierOverrideWasQueried, so that "if (applicationBundleIdentifierOverrideWasQueried)" reads like a sentence.

> Source/WebCore/platform/RuntimeApplicationChecks.mm:54
> +    const auto& identifier = applicationBundleIdentifierOverride();

Will this be true?

ASSERT(identifier.isNull() || RunLoop::isMain());
Comment 16 Chris Dumez 2016-03-11 11:35:17 PST
Created attachment 273749 [details]
Patch
Comment 17 Chris Dumez 2016-03-11 11:37:56 PST
Comment on attachment 273747 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=273747&action=review

>> Source/WebCore/platform/RuntimeApplicationChecks.mm:37
>> +static bool wasApplicationBundleIdentifierOverrideQueried = false;
> 
> Nitpick: I'd name this applicationBundleIdentifierOverrideWasQueried, so that "if (applicationBundleIdentifierOverrideWasQueried)" reads like a sentence.

Done.

>> Source/WebCore/platform/RuntimeApplicationChecks.mm:54
>> +    const auto& identifier = applicationBundleIdentifierOverride();
> 
> Will this be true?
> 
> ASSERT(identifier.isNull() || RunLoop::isMain());

It should. I added it.
Comment 18 WebKit Commit Bot 2016-03-11 12:25:57 PST
Comment on attachment 273749 [details]
Patch

Clearing flags on attachment: 273749

Committed r198040: <http://trac.webkit.org/changeset/198040>
Comment 19 WebKit Commit Bot 2016-03-11 12:26:04 PST
All reviewed patches have been landed.  Closing bug.