caoimhebyrne / KDiscordIPC

A Kotlin library for interacting with Discord via IPC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KDiscordIPC - Caught error when decoding packet (op: 0, length: 0) spammed

SIMULATAN opened this issue · comments

I'm using the Discord IntelliJ Plugin by Almighty Alpaca and seemingly randomly encounter my CPU usage spiking to 100% on a single core.
After some profiling and log reading, I found out that plenty of exceptions are occurring from KDiscordIPC.

Stack Trace
2023-05-04 14:22:45,086 [15294389] SEVERE - KDiscordIPC - Caught error when decoding packet (op: 0, length: 0) 
kotlinx.serialization.json.internal.JsonDecodingException: Cannot begin reading element, unexpected token: 10
JSON input: 
	at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
	at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
	at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:524)
	at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$default(AbstractJsonLexer.kt:523)
	at kotlinx.serialization.json.internal.JsonTreeReader.read(JsonTreeReader.kt:104)
	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeJsonElement(StreamingJsonDecoder.kt:33)
	at kotlinx.serialization.json.JsonContentPolymorphicSerializer.deserialize(JsonContentPolymorphicSerializer.kt:89)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
	at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100)
	at dev.cbyrne.kdiscordipc.core.packet.pipeline.ByteToMessageDecoder.decode(ByteToMessageDecoder.kt:23)
	at dev.cbyrne.kdiscordipc.core.socket.handler.SocketHandler$events$1.invokeSuspend(SocketHandler.kt:36)
	at dev.cbyrne.kdiscordipc.core.socket.handler.SocketHandler$events$1.invoke(SocketHandler.kt)
	at dev.cbyrne.kdiscordipc.core.socket.handler.SocketHandler$events$1.invoke(SocketHandler.kt)
	at kotlinx.coroutines.flow.SafeFlow.collectSafely(Builders.kt:61)
	at kotlinx.coroutines.flow.AbstractFlow.collect(Flow.kt:230)
	at kotlinx.coroutines.flow.internal.ChannelFlowOperatorImpl.flowCollect(ChannelFlow.kt:195)
	at kotlinx.coroutines.flow.internal.ChannelFlowOperator.collectTo$suspendImpl(ChannelFlow.kt:157)
	at kotlinx.coroutines.flow.internal.ChannelFlowOperator.collectTo(ChannelFlow.kt)
	at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:60)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

These exceptions are being spammed hundreds of times per second and end up clogging the log system.
log frequency
total amount of exceptions logged

profiling data

image

image

image

image

the logging system is in the DefaultDispatcher-worker-9
recorded over a duration of 15 seconds

Thanks for the detailed report. I just have a few more questions:

  • What operating system are you using?
  • Is discord or your IDE running in any sort of sandbox environment?
  • Can you reproduce this issue 100% of the time?

wow, that was fast!

  1. Arch Linux on an Aorus 15P KD
    I put more details in this gist

  2. Discord is running "natively", it was installed from the official Arch Linux repositories using pacman, so no flatpak or snap here.
    It happens in multiple JetBrains IDEs (PHPStorm, IntelliJ, GoLand, WebStorm (?)) and they were all installed by JetBrains Toolbox. They just run a normal JVM, other than that, I don't know of any sandbox (again, no flatpak or snap, it's as native as java gets 😉)

  3. No, it occurs rather occasionally, like twice a week roughly, but I did notice that it pretty much only happens when the IDE has been idling for some time. Once it happens, it full sends as seen in the original issue though.

Sorry for the loose root cause analysis, I unfortunately haven't found any sort of pattern except for what I stated above.

Thanks, I'll install arch on my machine and see if I can reproduce this.

Update on this, I've used:

  • Arch Linux
  • NixOS
  • Fedora Workstation 38
    and KDiscordIPC performs as expected on these, if you can get the issue to show up again, please re-open the issue.
commented

Hi, I am currently able to reproduce the error. This happened after I forced closed discord, and running cat /run/user/1000/discord-ipc-0 produces

cat: /run/user/1000/discord-ipc-0: No such device or address

Additionally, launching discord again does not fix the issue.
I am running the latest version of discord as of 19/08/2023, along with Vencord.

Performing rm -rf /run/user/1000/discord-ipc-0 stopped the errors from being produced.

I believe the error is being caused by KDiscordIPC being able to find the file, as it still exists, however it is unnable to decode the data properly as discord is no longer actually using the socket, as it was leaked/is hanging.

I believe that the fix for this would be to disconnect from the socket if it cannot read any data and the socket is unconnected.

commented

Update: after rm -rfing the file and re-opening discord, the issue is reproduced. Will look into this further

commented

Closing both intellij & discord, then re-opening them seems to have fixed the issue for now (?)

commented

But yeah, I more or less believe that this is just caused by the socket being hanging/leaked, and thus always returning empty data, causing deserialization exceptions.

This has been fixed in 0.2.2.