wiz0u / WTelegramClient

Telegram Client API (MTProto) library written 100% in C# and .NET
https://wiz0u.github.io/WTelegramClient/
MIT License
956 stars 156 forks source link

Session serialization error #248

Closed devoplot closed 4 months ago

devoplot commented 4 months ago

I faced with the strange error that occasionally happened :

System.Text.Json.JsonException: The object or value could not be serialized. Path: $.
System.InvalidOperationException: Cannot write the start of an object/array after a single JSON value or outside of an existing closed object/array. Current token type is 'EndObject'.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource, Int32 currentDepth, Int32 maxDepth, Byte token, JsonTokenType tokenType)
....
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at WTelegram.Session.Save()
at WTelegram.Client.NewMsgId(Boolean isContent)
at WTelegram.Client.SendAsync(IObject msg, Boolean isContent, Rpc rpc)
at WTelegram.Client.Invoke[T](IMethod`1 query)

but right now I see it almost on each API call.

I use WTelegramClient" 4.0.0 ( tried as well 4.0.1) and PostgreSQL to store session as in example https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_Heroku.cs

wiz0u commented 4 months ago

Do you have the full stack? Which version of .NET your program is running under ? Maybe the logs also

wiz0u commented 4 months ago

ok I think I found the problem. What you describe is actually a side-effect of having the SessionStore.Write throw an exception (pretty bad behavior for production code - Heroku example is just a demo, not production-grade code).

This means you likely have had a DB error happening before that exception. Check your logs. That DB exception messed with the internal state of session JsonWriter which then fails on all future calls to Session.Save.

In any case, I will harden Session.Save to prevent the exception you described. But you should seek to resolve your DB writing problems.