Skip to content

fix: remove force-unwrap on session dictionary in DefaultNetworkService - #1787

Merged
jglogan merged 1 commit into
apple:mainfrom
SEPURI-SAI-KRISHNA:fix/network-service-dict-force-unwrap
Jun 23, 2026
Merged

fix: remove force-unwrap on session dictionary in DefaultNetworkService#1787
jglogan merged 1 commit into
apple:mainfrom
SEPURI-SAI-KRISHNA:fix/network-service-dict-force-unwrap

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

In DefaultNetworkService.allocate, the dictionary entry for session
is initialized at line 98 if nil, then immediately force-unwrapped at
line 104 with allocationsBySession[session]!.append(...).

While the actor serializes access and the entry should always exist at
that point, the force-unwrap is an unnecessary crash risk if the
invariant is ever broken. Replaced with optional chaining (?) which
silently no-ops rather than crashing.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

@jglogan jglogan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@SEPURI-SAI-KRISHNA See the suggested fix for this force-try.

}
}
allocationsBySession[session]!.append((hostname: hostname, index: index))
allocationsBySession[session]?.append((hostname: hostname, index: index))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This approach to L98-104 would be better:

        let isNewSession = allocationsBySession[session] == nil
        allocationsBySession[session, default: []].append((hostname: hostname, index: index))
        if isNewSession {
            await session.onDisconnect { [weak self] in
                await self?.releaseSession(session)
            }
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have made the changes. please check. Thank you !

@SEPURI-SAI-KRISHNA
SEPURI-SAI-KRISHNA force-pushed the fix/network-service-dict-force-unwrap branch 3 times, most recently from da2da1d to c8d6756 Compare June 23, 2026 04:53
@SEPURI-SAI-KRISHNA
SEPURI-SAI-KRISHNA force-pushed the fix/network-service-dict-force-unwrap branch from c8d6756 to ef8e0b5 Compare June 23, 2026 04:55
@github-actions

Copy link
Copy Markdown

Code Coverage

Tier Line Coverage
Unit 33.36%
Integration 23.04%
Combined 55.56%

@jglogan jglogan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@SEPURI-SAI-KRISHNA approved, thank you!

@jglogan
jglogan merged commit 5e125d8 into apple:main Jun 23, 2026
3 checks passed
@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor Author

@jglogan Thanks for your support and patience. Would love to contribute further.

stephenlclarke pushed a commit to stephenlclarke/container that referenced this pull request Jun 24, 2026
…ce (apple#1787)

- Instead of using force-unwrap to append to a list-valued
  dictionary entry that should always exist, assign the value
  with a default fallback and append to the (non-optional)
  result.
jianliang00 pushed a commit to jianliang00/container that referenced this pull request Aug 28, 2026
…ce (apple#1787)

- Instead of using force-unwrap to append to a list-valued
  dictionary entry that should always exist, assign the value
  with a default fallback and append to the (non-optional)
  result.
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.

2 participants