Skip to content

feat: send the instance name in PRELOGIN and allow a port with an instance name - #1856

Draft
arthurschreiber wants to merge 3 commits into
masterfrom
claude/open-prs-review-dependencies-uiz27p-instance-name
Draft

arthurschreiber wants to merge 3 commits into
masterfrom
claude/open-prs-review-dependencies-uiz27p-instance-name

Conversation

@arthurschreiber

@arthurschreiber arthurschreiber commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

This ports the parts of #1491 (by @MichaelSun90 and @mShan0) that haven't landed yet onto current master. The routing part of #1491 (keeping the instance from a redirect target and sending server\instance in LOGIN7) already landed in #1668.

The PRELOGIN part was later narrowed to match what Microsoft.Data.SqlClient does. See "Findings" below.

Changes

port together with instanceName. This combination used to throw Port and instanceName are mutually exclusive. Now both are accepted: the port is used directly, no SQL Server Browser lookup takes place, and the instance name is ignored. This is how SqlClient treats server\instance,port. With only instanceName, the port is looked up as before. With neither, the port defaults to 1433.

Instance name in PRELOGIN, to verify a Browser lookup. When the port was looked up via the SQL Server Browser, the instance name is sent in the PRELOGIN INSTOPT option, null-terminated. If the server answers 0x01 (it is a different instance), the connection fails with a ConnectionError with code EINSTLOOKUP. The message names the server, the port and the instance.

In every other case an empty instance name (0x00) is still sent, as before. The server always accepts an empty name. These cases are:

  • an explicit port
  • after a redirect, because routing data always carries a port
  • MSSQLSERVER, in any case
  • instance names that contain anything other than printable ASCII

Compared with #1491:

  • The server's INSTOPT response is read as the single status byte the protocol defines, not as a string.
  • Only 0x01 counts as a mismatch, as in SqlClient.
  • No instance name is sent with an explicit port or after a redirect.
  • A mismatch uses the existing EINSTLOOKUP code instead of a new EINSTANCE code.
  • The comment-only changes to instance-lookup.ts and sender.ts are left out.

Findings

Sources:

  • MS-TDS (linked below).
  • Microsoft.Data.SqlClient at commit ce73774. Paths are under src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/.
  • mssql-jdbc at commit a010732. Paths are under src/main/java/com/microsoft/sqlserver/jdbc/.

Spec. MS-TDS 2.2.6.5 PRELOGIN:

  • The client SHOULD send the instance name "if available", as a null-terminated MBCS string.
  • The server answers 0 if the string is empty, or if it matches the instance name or "MSSQLServer" case-insensitively, using the server's locale. Otherwise it answers 1.
  • The client SHOULD terminate the connection on 1.
  • Footnote 40: SQL Server 2000 through 2008 R2 do not accept "MSSQLServer" for a default instance. Only later versions do.

a. Which instance name do drivers send?

  • SqlClient, managed SNI (the default on Linux and macOS): always sends an empty instance name.
    • ManagedSni/SniProxy.netcore.cs line 68: CreateConnectionHandle sets instanceName = new byte[1] and never fills it.
    • Same file, lines 582–616: DataSource parsing ignores the instance when a port is given, and rejects server\mssqlserver.
  • SqlClient, native SNI (Windows): the name comes from closed-source native code.
    • NativeSni/SNIHandle.cs line 37 passes a 256-byte buffer that the native code fills. What it writes for a default instance, with an explicit port, or after a redirect can't be read from the source.
    • Parser/TdsParser.cs lines 1336–1347, in ConsumePreLoginHandshake: the comment gives the purpose of the check. A mismatch means "the cached info we used to connect is not valid", in other words a stale SSRP (Browser) result.
  • mssql-jdbc: doesn't send INSTOPT at all. SQLServerConnection.java lines 4593–4598: its PRELOGIN request has only VERSION, ENCRYPTION, TRACEID and optionally FEDAUTHREQUIRED.
  • tedious, now: sends the name only when it was used for a Browser lookup, which matches the purpose of the check in SqlClient. It doesn't fail a connection that managed SqlClient or JDBC would make with an explicit port, after a redirect, or with MSSQLSERVER.

b. Encoding. The spec says MBCS, compared in the server's locale. The client can't know the server's code page, so only printable-ASCII names are sent. ASCII encodes the same way in the relevant code pages. Any other name is sent empty, which the server always accepts. In SqlClient, the encoding is done in native SNI and isn't visible.

c. Response byte, and retries.

  • SqlClient fails only when the response byte is exactly 0x01 (Parser/TdsParser.cs lines 1336–1347). tedious now does the same.
  • On a failure, SqlClient retries once (Parser/TdsParser.cs lines 575–636). The retry opens a new connection with flushCache: true, so the SSRP lookup runs again, and it sends the instance name again. If that also fails, it throws SQL.InstanceFailure() ("Instance failure.").
  • No driver retries without the instance name.
  • tedious doesn't cache Browser lookups, so it fails right away instead of retrying.

d. Error code. An instance name is now only sent to verify a Browser result, so a mismatch means the lookup returned the port of a different instance. The existing EINSTLOOKUP code fits that, so no new code is added. SqlClient reports it as a generic InvalidOperationException.

Remaining behavior change

When instanceName is set without a port, tedious now sends that name. A connection fails with EINSTLOOKUP if the server at the looked-up port says it is a different instance. Before this change, such a connection would have gone on to log in to whichever instance was listening there.

Coverage against a real server

  • AppVeyor runs on this PR. It runs the whole integration suite, at TDS 7.4, 7.3B, 7.3A, 7.2 and 7.1, against the named instance SQL2017. It connects through a Browser lookup with no port (test/config.appveyor.ts), which is exactly the path that now sends INSTOPT. The suite passes on Node.js 22, 24 and 26 for dcd0ffe (build).
  • GitHub Actions only connects to default instances on a fixed port: Linux containers, and Windows through setup-sqlserver. Adding a named instance there would mean installing a second SQL Server instance and starting the SQL Browser on the Windows runners. That is a larger CI change and isn't part of this PR.
  • Not covered against a real server: a mismatch response, and a named instance on SQL Server 2008 R2 or older.

Testing

Unit tests:

  • prelogin-payload-test.ts: encoding the instance name, and reading a rejecting response.
  • connection-config-validation.ts: port defaults, and port together with instanceName.
  • connection-instance-name-test.ts (new), against a mock server with a stubbed Browser lookup:
    • a looked-up port sends the instance name
    • a server answering 0x01 fails with EINSTLOOKUP
    • an explicit port skips the lookup and sends an empty name
    • MSSQLSERVER, mssqlserver and a non-ASCII name are sent empty
  • rerouting-test.ts: after a redirect, an empty instance name is sent.

Locally, npm test (754 passing) and npm run lint (eslint + tsc) pass. In CI, all GitHub Actions jobs and AppVeyor are green on dcd0ffe.

…tance name

Port the remaining parts of #1491 to current master:

- Send the configured instance name (or, after a redirect, the routing
  target's instance name) in the PRELOGIN `INSTOPT` option, and fail the
  connection with `EINSTANCE` if the server rejects it.
- Allow `port` and `instanceName` to be set together. The port is then used
  directly, without a SQL Server Browser lookup, and the instance name is
  still sent for the server to verify.

Co-authored-by: MichaelSun90 <47126816+MichaelSun90@users.noreply.github.com>
Co-authored-by: mShan0 <96149598+mShan0@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UtKcuXMU8g8KvcVkzpxu8q
Match what Microsoft.Data.SqlClient and the MS-TDS spec do with `INSTOPT`:

- Send the instance name only when the port was looked up via the SQL
  Server Browser. With an explicit port, or after a redirect, send an
  empty instance name, so connections that work today keep working.
  SqlClient's managed networking never sends an instance name, and
  mssql-jdbc doesn't send `INSTOPT` at all.
- Don't send `MSSQLSERVER`, which SQL Server 2008 R2 and older reject, or
  names that aren't printable ASCII, as the server compares them in its
  own code page.
- Treat only `0x01` as a mismatch, like SqlClient, and report it as
  `EINSTLOOKUP` instead of a new `EINSTANCE` code, since it means the
  Browser returned a port of a different instance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UtKcuXMU8g8KvcVkzpxu8q
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.43%. Comparing base (61fb184) to head (57d2661).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1856      +/-   ##
==========================================
+ Coverage   85.24%   85.43%   +0.19%     
==========================================
  Files          97       97              
  Lines        4778     4787       +9     
  Branches      896      900       +4     
==========================================
+ Hits         4073     4090      +17     
+ Misses        447      441       -6     
+ Partials      258      256       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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