fix: use ordinal string comparison for deterministic ordering#273
Open
YunchuWang wants to merge 1 commit into
Open
fix: use ordinal string comparison for deterministic ordering#273YunchuWang wants to merge 1 commit into
YunchuWang wants to merge 1 commit into
Conversation
Replace localeCompare() with < / > operators in lockEntities() and compareVersions() for locale-independent, deterministic ordering that matches the .NET SDK's StringComparer.Ordinal behavior. Fixes #231 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a determinism issue in the Durable Task JS SDK by replacing locale-sensitive string comparisons with locale-independent ordinal comparisons, aligning behavior across platforms/locales (and with .NET’s StringComparer.Ordinal* semantics). This directly addresses issue #231, which highlighted potential non-deterministic ordering in entity lock acquisition and version comparison.
Changes:
- Updated
lockEntities()to sort entity IDs using ordinal string comparison instead oflocaleCompare(), ensuring deterministic lock ordering. - Updated
compareVersions()to use ordinal (case-insensitive) ordering for non-semver fallbacks instead oflocaleCompare(). - Added/updated unit tests to validate ordinal ordering behavior and prevent regressions.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| packages/durabletask-js/src/worker/runtime-orchestration-context.ts | Replaces locale-dependent entity ID sorting with deterministic ordinal comparison for lock ordering. |
| packages/durabletask-js/src/utils/versioning.util.ts | Replaces locale-dependent fallback version comparison with deterministic ordinal (case-insensitive) comparison. |
| packages/durabletask-js/test/entity-locking.spec.ts | Adds a regression test ensuring lockEntities() ordering is deterministic and not locale-dependent. |
| packages/durabletask-js/test/versioning.spec.ts | Updates/adds tests to ensure non-semver version comparisons are deterministic and not locale-dependent. |
Comment on lines
+866
to
+869
| // Sort entities for deterministic ordering (prevents deadlocks). | ||
| // Use ordinal (code-point) comparison for cross-platform consistency, | ||
| // matching .NET's StringComparer.Ordinal. localeCompare() is locale-dependent | ||
| // and can produce different orderings on different machines/locales. |
| expect(lockSet[2]).toBe("@counter@c"); | ||
| }); | ||
|
|
||
| it("should sort entities by ordinal (code-point) order, not locale order", async () => { |
Comment on lines
+154
to
+157
| // In many locales, localeCompare() sorts accented characters adjacent to their | ||
| // base letters (e.g., "ä" near "a"). Ordinal (code-point) comparison places | ||
| // them by their Unicode value instead. This ensures consistent cross-platform | ||
| // ordering that matches the .NET SDK's StringComparer.Ordinal. |
| expect(compareVersions("Alpha", "BETA")).toBeLessThan(0); | ||
| }); | ||
|
|
||
| it("should use ordinal (code-point) order, not locale order", () => { |
Comment on lines
+91
to
+93
| // In ordinal comparison, uppercase letters (U+0041-U+005A) come before | ||
| // lowercase letters (U+0061-U+007A) after lowercasing they are equal. | ||
| // But characters outside ASCII can differ between ordinal and locale: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #231