Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mac os sandbox check slowness #3879

Merged
merged 4 commits into from
Aug 23, 2024

Conversation

aboedo
Copy link
Member

@aboedo aboedo commented May 8, 2024

Alternative approach to #3875, also meant to address #3871

We're currently looking at a field called Environment in the local receipt to check whether the app is running in sandbox on macOS.

However, since it's an undocumented field, we can't trust that it will always be there, so if the value is production then we check the bundle's signature, which can be slow to run.

This PR changes that behavior so that we only perform that check if the value for that variable isn't available, which would cause a receipt parsing failure since it's marked as non-optional.

Still need to write tests for this, but it should be significantly easier than for the other PR.

@aboedo aboedo added the pr:fix A bug fix label May 8, 2024
@aboedo aboedo requested review from MarkVillacampa and a team May 8, 2024 21:49
@aboedo aboedo self-assigned this May 8, 2024
@@ -73,12 +80,12 @@ extension BundleSandboxEnvironmentDetector: Sendable {}

private extension BundleSandboxEnvironmentDetector {

var isProductionReceipt: Bool {
var isProductionReceipt: Bool? {
do {
return try self.receiptFetcher.fetchAndParseLocalReceipt().environment == .production
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check if the environment is .unknown? Seems that the catch will happen with any parsing error, not specifically about the environment so I wonder if we will be doing extra operations in those cases... Having said that, parsing errors should be fairly uncommon, so I think it should be ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only 3 months late to respond to the comment 😅
Good call, I'll add an extra check for that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't imagine it would happen often that we can correctly parse the receipt and it's production but it doesn't have the environment variable, but I can't discard it either since it's an undocumented field

… we can't verify the environment from the receipt itself
@aboedo aboedo force-pushed the origin/andy/fix_macOS_sandbox_check_slowness_2 branch from e1df30d to 31a8ba4 Compare August 22, 2024 22:00
@aboedo aboedo changed the title fix mac os sandbox check slowness 2 fix mac os sandbox check slowness Aug 22, 2024
@aboedo aboedo marked this pull request as ready for review August 22, 2024 22:46
func testIsNotSandboxIfReceiptIsProductionAndMAS() throws {
func testIsNotSandboxIfReceiptIsProduction() throws {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we no longer look at MAS if we know what the receipt environment is

Comment on lines -66 to -72
func testIsSandboxIfReceiptIsNotProductionAndNotMAS() throws {
expect(
SystemInfo.with(
macAppStore: false,
receiptEnvironment: .sandbox
).isSandbox
) == true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test and the next one are technically still there, but now in a more complete test that also verifies that we indeed use MAS when needed and skip it when not needed

Comment on lines -154 to +211
private struct MockMacAppStoreDetector: MacAppStoreDetector {
private final class MockMacAppStoreDetector: MacAppStoreDetector, @unchecked Sendable {

let isMacAppStore: Bool
let isMacAppStoreValue: Bool
private(set) var isMacAppStoreCalled = false

init(isMacAppStore: Bool) {
self.isMacAppStore = isMacAppStore
self.isMacAppStoreValue = isMacAppStore
}

var isMacAppStore: Bool {
isMacAppStoreCalled = true
return isMacAppStoreValue
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes here are to make it possible to check whether it was called

@aboedo
Copy link
Member Author

aboedo commented Aug 22, 2024

@tonidero @MarkVillacampa ready for another pass, hopefully it takes me less than 3 months this time to iterate on PR comments this time

@aboedo
Copy link
Member Author

aboedo commented Aug 22, 2024

Tested on device (by forcing the environment value in the receipt) and was able to verify that I got the warning before the changes and did not get it after.

Still could be optimized further by caching the value (it won't change in a single app run) but then I started fighting Sendable and figured to live to fight another day

@MarkVillacampa
Copy link
Member

MarkVillacampa commented Aug 23, 2024

Looking good!

Copy link
Contributor

@tonidero tonidero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@aboedo aboedo merged commit 3120e66 into main Aug 23, 2024
5 checks passed
@aboedo aboedo deleted the origin/andy/fix_macOS_sandbox_check_slowness_2 branch August 23, 2024 13:10
vegaro pushed a commit that referenced this pull request Aug 30, 2024
**This is an automatic release.**

### Bugfixes
* Fix `compatibleTopBarTrailing` in MacOS and api tests (#4226) via
Cesar de la Vega (@vegaro)
* [Paywall] Fix restoreStarted not being called on
`presentPaywallIfNeeded` when using `requiredEntitlementIdentifier`
(#4223) via Josh Holtz (@joshdholtz)
* [CustomerCenter] Move sheet and restore alert creation to
`ManageSubscriptionsView` (#4220) via Cesar de la Vega (@vegaro)
* [EXTERNAL] `Custom Entitlements Computation`: fix support display on
debug screen (#4215) by @NachoSoto (#4218) via Toni Rico (@tonidero)
* [Customer Center] Add padding to `No thanks` in promotional offer
screen (#4221) via Cesar de la Vega (@vegaro)
* Fix version number in plist files (#4213) via Cesar de la Vega
(@vegaro)
* fix mac os sandbox check slowness (#3879) via Andy Boedo (@aboedo)
* [Customer Center] Fix `FeedbackSurveyView` not opening (#4208) via
Cesar de la Vega (@vegaro)
* Remove `unneeded_override` disable to fix linter (#4209) via Cesar de
la Vega (@vegaro)
### Dependency Updates
* Bump rexml from 3.3.3 to 3.3.6 in
/Tests/InstallationTests/CocoapodsInstallation (#4210) via
dependabot[bot] (@dependabot[bot])
* Bump rexml from 3.3.3 to 3.3.6 (#4211) via dependabot[bot]
(@dependabot[bot])
### Other Changes
* Update readme wording (#3914) via James Borthwick (@jamesrb1)
* Set a maximum duration for iOS 15 tests (#4229) via Cesar de la Vega
(@vegaro)
* Paywall Components Initial Commit (#4224) via James Borthwick
(@jamesrb1)
* [CustomerCenter] Open App Store when the user wants to update their
app (#4199) via JayShortway (@JayShortway)
* [Customer Center] Shows a warning when the app is not the latest
version (#4193) via JayShortway (@JayShortway)
* Fix integration tests simulator version (#4219) via Cesar de la Vega
(@vegaro)
* Pin swift-docc-plugin to 1.3.0 (#4216) via James Borthwick (@jamesrb1)
nyeu pushed a commit that referenced this pull request Oct 2, 2024
Alternative approach to #3875, also meant to address
#3871

We're currently looking at a field called Environment in the local
receipt to check whether the app is running in sandbox on macOS.

However, since it's an undocumented field, we can't trust that it will
always be there, so if the value is production then we check the
bundle's signature, which can be slow to run.

This PR changes that behavior so that we only perform that check if the
value for that variable isn't available, which would cause a receipt
parsing failure since it's marked as non-optional.

Still need to write tests for this, but it should be significantly
easier than for the other PR.
nyeu pushed a commit that referenced this pull request Oct 2, 2024
**This is an automatic release.**

### Bugfixes
* Fix `compatibleTopBarTrailing` in MacOS and api tests (#4226) via
Cesar de la Vega (@vegaro)
* [Paywall] Fix restoreStarted not being called on
`presentPaywallIfNeeded` when using `requiredEntitlementIdentifier`
(#4223) via Josh Holtz (@joshdholtz)
* [CustomerCenter] Move sheet and restore alert creation to
`ManageSubscriptionsView` (#4220) via Cesar de la Vega (@vegaro)
* [EXTERNAL] `Custom Entitlements Computation`: fix support display on
debug screen (#4215) by @NachoSoto (#4218) via Toni Rico (@tonidero)
* [Customer Center] Add padding to `No thanks` in promotional offer
screen (#4221) via Cesar de la Vega (@vegaro)
* Fix version number in plist files (#4213) via Cesar de la Vega
(@vegaro)
* fix mac os sandbox check slowness (#3879) via Andy Boedo (@aboedo)
* [Customer Center] Fix `FeedbackSurveyView` not opening (#4208) via
Cesar de la Vega (@vegaro)
* Remove `unneeded_override` disable to fix linter (#4209) via Cesar de
la Vega (@vegaro)
### Dependency Updates
* Bump rexml from 3.3.3 to 3.3.6 in
/Tests/InstallationTests/CocoapodsInstallation (#4210) via
dependabot[bot] (@dependabot[bot])
* Bump rexml from 3.3.3 to 3.3.6 (#4211) via dependabot[bot]
(@dependabot[bot])
### Other Changes
* Update readme wording (#3914) via James Borthwick (@jamesrb1)
* Set a maximum duration for iOS 15 tests (#4229) via Cesar de la Vega
(@vegaro)
* Paywall Components Initial Commit (#4224) via James Borthwick
(@jamesrb1)
* [CustomerCenter] Open App Store when the user wants to update their
app (#4199) via JayShortway (@JayShortway)
* [Customer Center] Shows a warning when the app is not the latest
version (#4193) via JayShortway (@JayShortway)
* Fix integration tests simulator version (#4219) via Cesar de la Vega
(@vegaro)
* Pin swift-docc-plugin to 1.3.0 (#4216) via James Borthwick (@jamesrb1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:fix A bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants