KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
697 stars 230 forks source link

Printing self referencing lexicon crashes KSP #1598

Open thexa4 opened 8 years ago

thexa4 commented 8 years ago

The following code immediately crashes KSP for me using version 0.19.3:

local foo to lexicon().
set foo["self"] to foo.

print foo.

Lists have the same problem:

local foo to list(0).
set foo[0] to foo.

print foo.

Nothing seems to be written to the KSP log when it crashes. Probably related to #1379

Due to this things like the following code crash as well, making debugging very hard as any print call can crash KSP:

local foo to lexicon().
set foo["self"] to foo.

function bar { parameter x. }
local baz to bar@:bind(foo).
print baz.
tomekpiotrowski commented 8 years ago

We are aware of this issue. There is currently no detection of cyclic references in a few places.

thexa4 commented 8 years ago

That's good to know, I'm currently trying to work with code like this which prevents me from printing anything at all.

local foo to lexicon("count", 0).
function bar { parameter this. set this["count"] to this["count"] + 1. }
set foo["inc"] to bar@:bind(foo).

print foo["inc"]. //This crashes

Replacing the toString function on kOS.Safe.Encapsulation.KOSDelegate.cs:146 with return "KOSDelegate"; seems to make debugging workable again but is probably not a long term solution.

abesto commented 2 years ago

Hallo! I just hit this same problem today. It'd be really really great if a fix for this could be merged, even if it's not a full-fledged fancy pretty-printer solution. I know +1s on bug report threads are un-useful, but I see the latest activity here was about half a year ago; and I know basically nothing about KSP modding internals, so best I can do ATM is make some noise :)

For context: I'm doing some minimal extra-light OOP to hold data and related methods in Lexicons where (obviously) the "methods" are all some f@:bind(self) (same as #2918). This means that there are a ton of mistakes I can make in writing code that lead to kOS dumping a Lexicon, which then immediately causes a CDT :(

thexa4 commented 2 years ago

I have a branch tries to fix this but it isn't well tested, it's available here: https://github.com/thexa4/KOS/releases/tag/dev-2022-01-12 It'd be great to get feedback on this (or on the standalone version)

abesto commented 2 years ago

@thexa4 ah lovely, I saw your fork / PRs but didn't realize you'd made a compiled version. I'll give it a spin... today evening? Worst case over the weekend, and report back.

abesto commented 2 years ago

@thexa4 I grabbed the kOS.dll and kOS.Safe.dll from your release, plopped them into the right folder in my installation, and happy to report the craft that was consistently crashing the game is now consistently not-crashing the game, and instead giving me a nice, helpful error message:

image

I'll keep trying to do nasty things to kOS scripts and see if I manage to break it, but at least at first glance, on the case that brought me here, your build seems like a fix.

(and btw yes, insult to injury, my mistake in the code was the dumbest typo ever)

abesto commented 2 years ago

Alrighty, so: I've now done all the over-engineering I had in mind - there's unit testing, there's pieces of a higher-level GUI library, and a cross-core / cross-vessel RPC implementation (https://github.com/abesto/kos-scripts/blob/main/lib/rpc.ks). Through all this I didn't have a single crash, and error outputs were always valid.