dotnet / dotnet/orleans

Wrong persistent state loaded for grain with all-digit string key

Open
#9,706 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.9k
Forks
2.1k
Avg merge
13h 56m
Merged PRs (30d)
351

Description

# Describe the bug
Grains with string keys consisting of only digits and leading zeros may load the state for a different grain from the persistent store.
For example, the call `GetGrain("020235")` will activate the grain with string key "020235", but loads the persistent state for grain "20235".

This seems to be caused by the ADO.NET provider interpreting the grain key as an int, because it can parse it as such.

```
Utf8Parser.TryParse(Encoding.UTF8.GetBytes("20235"), out long key1, out _, 'X');
Utf8Parser.TryParse(Encoding.UTF8.GetBytes("020235"), out long key2, out _, 'X');
Console.WriteLine("Equal: " + (key1 == key2)); // Prints true
```
Also discussed on Discord: https://discord.com/channels/333727978460676096/333727978460676096/1423619403022336080

# To Reproduce
1. Activate a grain of type T with string key "20235" and write its persistent state,
2. Activate a grain of type T with string key "020235" and inspect the loaded state, which will be the state for grain "20235".

# Workaround
Make sure the string key contains at least one non-digit to ensure it cannot be parsed as a number.
For example by prefixing the numeric string key: `GetGrain("+020235")`

# Further technical details
ADO.NET grain storage provider on SQL Server
Orleans 9.2.1

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the two-step case with the ADO.NET grain storage provider on SQL Server, using keys "20235" and "020235". Trace how the provider interprets and persists the grain key, then verify that each key loads only its own state and that the numeric parsing behavior no longer conflates them.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.