Skip to content

Create background Hub clients only if necessary#337

Closed
atdrendel wants to merge 1 commit intohuggingface:mainfrom
shareup:lazy-network-monitor
Closed

Create background Hub clients only if necessary#337
atdrendel wants to merge 1 commit intohuggingface:mainfrom
shareup:lazy-network-monitor

Conversation

@atdrendel
Copy link
Contributor

@atdrendel atdrendel commented Mar 17, 2026

This pull request does two things:

  • Only create background Hub clients (i.e., Hub clients that use background URL sessions) if HubApi.init is called with useBackgroundSession: true, otherwise reuse the foreground sessions
  • Simplify the use NetworkMonitor and remove its duplicate initialization

When background URL sessions are initialized in applications that do not include the com.apple application-identifier entitlement, the system logs an error.

Before removing background URL sessions

After applying this fix, the error is no longer logged.

After removing background URL sessions

Copilot AI review requested due to automatic review settings March 17, 2026 22:24
)
) : self.foregroundUncachedClient
#endif
NetworkMonitor.shared.startMonitoring()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not need to manually call startMonitoring() because NetworkMonitor.init() calls it.

Copy link
Collaborator

@mattt mattt Mar 23, 2026

Choose a reason for hiding this comment

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

This seems like the correct fix — especially if we're keeping shared instance around. Related discussion in #259.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts HubApi initialization so background URLSession-backed Hub clients are only created when useBackgroundSession: true, avoiding system log noise in apps lacking the required entitlement. It also refactors NetworkMonitor usage to a singleton to prevent duplicate initialization.

Changes:

  • Conditionally build background Hub clients only when useBackgroundSession is enabled; otherwise reuse the foreground clients.
  • Switch NetworkMonitor to a shared singleton with a private initializer and remove the redundant external startMonitoring() call.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces unwanted system log errors by preventing background URLSession creation unless explicitly requested, and consolidates network monitoring to a single shared instance.

Changes:

  • Conditionally build background HubClient instances only when HubApi.init(useBackgroundSession: true) is used; otherwise reuse foreground clients.
  • Remove redundant NetworkMonitor initialization by using NetworkMonitor.shared and making its initializer private.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mattt
Copy link
Collaborator

mattt commented Mar 23, 2026

Thanks for opening this, @atdrendel! There's a formatting issue that's failing early in CI. I tried to apply the change, but it looks like your fork isn't configured to allow changes from downstream, so I opened #338 with that.

@mattt
Copy link
Collaborator

mattt commented Mar 23, 2026

Just merged #338 with these changes. Thanks again, @atdrendel!

@mattt mattt closed this Mar 23, 2026
@atdrendel atdrendel deleted the lazy-network-monitor branch March 23, 2026 09:26
@mattt
Copy link
Collaborator

mattt commented Mar 23, 2026

This is now available in swift-transformers 1.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants