Vscode-swift hits network every 2s (via swiftly)

While looking at my network traffic on Proxyman, I discovered a huge number of calls to https://www.swift.org/api/v1/swiftly.json (one every 2s)

Turns out, if you have the VSCode Swift extension installed, it shells out to swiftly use --format=json every 2s, in an infinite loop. And Swiftly unconditionally makes this network request when invoked.

Even if it made sense to spawn swiftly every two seconds to determine local state, the network request achieves nothing here: it's used to display "A new release of swiftly is available", but this invocation isn't even user facing.

My 2c for a short-term fix would be to plumb through a --disable-update-check flag to swiftly, and/or have it check if the terminal is a tty first, and/or have it cache the check result for at least 30 minutes. Happy to PR the fixes, but not sure which of these options is preferred. Thoughts?

9 Likes

@kabiroberai yikes, thanks for highlighting this.

2 Likes

In fact it appears that swiftly makes a network connection every time you invoke swiftly run, which is a problem whenever WiFi is wonky or misconfigured on Linux (as happens!), because then it can hang for minutes at a time:

$ strace -f swiftly run echo 2>&1 | grep connect
[pid 121526] connect(79, {sa_family=AF_INET6, sin6_port=htons(65535), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "2620:149:a33:f000::133", &sin6_addr), sin6_scope_id=0}, 28 <unfinished ...>
[pid 121526] <... connect resumed>)     = 0
[pid 121526] connect(79, {sa_family=AF_INET6, sin6_port=htons(65535), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "2620:149:a33:f000::148", &sin6_addr), sin6_scope_id=0}, 28 <unfinished ...>
[pid 121526] <... connect resumed>)     = 0
[pid 121526] connect(79, {sa_family=AF_INET, sin_port=htons(65535), sin_addr=inet_addr("17.253.3.132")}, 16 <unfinished ...>
[pid 121526] <... connect resumed>)     = 0
[pid 121526] connect(79, {sa_family=AF_INET, sin_port=htons(65535), sin_addr=inet_addr("17.253.3.145")}, 16) = 0
[pid 121498] getpeername(79, 0x7a6a2664de30, [128]) = -1 ENOTCONN (Transport endpoint is not connected)
[pid 121498] connect(79, {sa_family=AF_INET6, sin6_port=htons(443), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "2620:149:a33:f000::133", &sin6_addr), sin6_scope_id=0}, 28) = -1 EINPROGRESS (Operation now in progress)
$ host 17.253.3.132
132.3.253.17.in-addr.arpa domain name pointer usnyc3-vip-fx-101.b.aaplimg.com.
$ host 17.253.3.145
145.3.253.17.in-addr.arpa domain name pointer usnyc3-vip-fx-108.a.aaplimg.com.
$ whois aaplimg.com | grep 'Name Server'
   Name Server: A.NS.APPLE.COM
   Name Server: B.NS.APPLE.COM
   Name Server: C.NS.APPLE.COM
   Name Server: D.NS.APPLE.COM
Name Server: a.ns.apple.com
Name Server: b.ns.apple.com
Name Server: c.ns.apple.com
Name Server: d.ns.apple.com

I think it would be better if swiftly only checked for self-updates when you ran a command which must make a network connection by design, like list-available or install.

4 Likes

It could also probably find a way to remember that it’s already checked within, say, the last hour

1 Like

Oof yeah this isn't ideal. I can expand on why this is happening.

  • Every 2 seconds, the vscode-swift extension calls swiftly to check if the in-use toolchain has changed
  • Every swiftly invocation performs an update check, as Slava noted.

I agree Swiftly should not check for updates in every command; this ask was brought up in Commands that do not need network access always start outbound connections · Issue #586 · swiftlang/swiftly · GitHub.

I'd also like a flag on all the commands in swiftly that perform an update check like --skip-update-check, because the VS Code Swift extension is effectively calling swiftly heedlessly and should never invoke the upgrade path. I created Add a `--skip-update-check` flag on commands that call `validateSwiftly` · Issue #614 · swiftlang/swiftly · GitHub to track this.

Finally, as a short term fix in the vscode-swift extension, I've put up Watch swiftly's config.json instead of polling swiftly use by plemarquand · Pull Request #2428 · swiftlang/vscode-swift · GitHub which stops invoking swiftly to check if the in-use toolchain has changed and instead installs a file watcher on the config.json file where the in-use version is stored. I hope to get this out in the next extension release.

We'll use this method when the user has older versions of swiftly installed, and if the --disable-update-check flag or similar lands in a newer version we'll switch back to the previous approach which doesn't rely as much on knowing how and where swiftly stores information about the in-use toolchain.

3 Likes

I'm looking at a fix for Swiftly. Probably follow what brew, gh, pip, and a bunch of other tools do and cache the result of the check for 24 hours before looking again. Certain commands that inherently make network requests like swiftly install and swiftly list-available may need to check more frequently, but swiftly use and swiftly run certainly shouldn't need to check all of the time.

There are implications to a switch that entirely disables the version check that may preclude adding something like that.

I think caching this request makes a lot of sense, but I'm curious what would prevent a switch that disables network access on commands that don't require it.

A few use cases I can think of are clients like VS Code that call swiftly, that wouldn't want to update swiftly behind the user's back, and environments where you may want to use swiftly but not have internet connectivity, where you'd have to wait for the update check to timeout on every invocation.

I don't think those commands need to check at all, even once a day could potentially be annoying with a bad network connection. There's also no indication that it's doing a self-update check, it just appears to hang. When I first encountered the problem I would ^C swiftly, run it again, see it hang again, before eventually giving up and running swiftc from the toolchain directory. Some users might not know how to do that, and conclude that swift(ly) is just broken. There's also the privacy angle.

6 Likes

Synchronous network request on main thread? :face_with_open_eyes_and_hand_over_mouth:

It is async, but await'd before the action takes place.