[boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Bugs that are actually features.
Post Reply
robot256
Filter Inserter
Filter Inserter
Posts: 594
Joined: Sun Mar 17, 2019 1:52 am
Contact:

[boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by robot256 »

Using Space Exploration mod version 0.5.93 (for example), I tried to make a mod subscribe to the custom mod event for when cargo rockets launch. This custom event is intended to include a large data structure including primitives and LuaObject references. No events were generated, even though `raise_event` was being called with the correct custom event number. When I changed the data structure passed to raise_event() to contain only a few levels of nested tables (including primitives and LuaObject references), the event was raised correctly.

This can be confirmed by starting Space Exploration, running the following console command, triggering a cargo rocket launch, and seeing no message appear:

Code: Select all

/c script.on_event(remote.call("space-exploration", "get_on_cargo_rocket_launched_event"), function(event) game.print("Rocket Launched!") end)
I am not at all surprised that the game engine rejects such a large data structure in an event table. But since it fails silently, this bug has existed in the mod for a long time (since no one actually used the custom event) and it took me a while to figure out. It would be nice if this behavior caused a runtime error, or was at least mentioned in the documentation.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2227
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by boskid »

I checked your reproduction steps but everything works fine - it prints the message. Please provide a better reproduction that shows the issue you have.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13171
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Rseding91 »

Could it be that one or more of the mod you're using is invalidating the LuaObject(s) you're passing resulting in the event chain ending?
If you want to get ahold of me I'm almost always on Discord.

robot256
Filter Inserter
Filter Inserter
Posts: 594
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by robot256 »

You are correct, it does not happen in a new game, only in my 1000-hour save. The "struct" argument includes links to multiple entities, but they should all be permanent (since the launchpad continues to function after the first launch). Earendel suspected it was because of the size of Zone data structure, which is hard to replicate.

The following steps work for me:

1. Load the save from https://drive.google.com/file/d/1B5mcMZ ... sp=sharing (418MB)
2. Run

Code: Select all

/c script.on_event(remote.call("space-exploration", "get_on_cargo_rocket_launched_event"), function(event) game.print(tostring(game.tick)..": Rocket Launched!") end)
3. Run

Code: Select all

/c script.raise_event(remote.call("space-exploration","get_on_cargo_rocket_launched_event"),{struct={"hello world"}})
4. Message is printed.
5. Manually launch the rocket nearest the character (or any of them).
6. No message is printed.

Thanks for looking into this.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13171
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Rseding91 »

Running it in the debugger: one of the entities being passed in the event is invalid and so the data does not get passed.
If you want to get ahold of me I'm almost always on Discord.

robot256
Filter Inserter
Filter Inserter
Posts: 594
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by robot256 »

Understood, thanks! I can look into that some more and see what got corrupted. The docs for raise_event didn't mention failing when an argument entity is invalid. (I didn't even think of that, because I'm so used to needing to check entity.valid inside event handlers.)

Rseding91
Factorio Staff
Factorio Staff
Posts: 13171
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Rseding91 »

robot256 wrote:
Fri Dec 24, 2021 7:00 pm
Understood, thanks! I can look into that some more and see what got corrupted. The docs for raise_event didn't mention failing when an argument entity is invalid. (I didn't even think of that, because I'm so used to needing to check entity.valid inside event handlers.)
Event parameters will never be invalid and so never need to be checked for .valid. The only time you have to check is if between you getting the reference and using it you allow some other mod or code to run that may invalidate the entity.
If you want to get ahold of me I'm almost always on Discord.

robot256
Filter Inserter
Filter Inserter
Posts: 594
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by robot256 »

That makes sense why I've gotten away with being sloppy lately. The crash reports always come from code that causes other events to trigger in the middle of my handler.

In this case, I traced the invalid entity references to two migrated entities that were not removed from the global data structure, and one reference to a temporary entity that was not purged at the time of its deletion. Reported to Earendel for a fix.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Optera »

Rseding91 wrote:
Fri Dec 24, 2021 11:38 pm
robot256 wrote:
Fri Dec 24, 2021 7:00 pm
Understood, thanks! I can look into that some more and see what got corrupted. The docs for raise_event didn't mention failing when an argument entity is invalid. (I didn't even think of that, because I'm so used to needing to check entity.valid inside event handlers.)
Event parameters will never be invalid and so never need to be checked for .valid. The only time you have to check is if between you getting the reference and using it you allow some other mod or code to run that may invalidate the entity.
I have a question about this.

Let's say my mod does something like this:

Code: Select all

global.trains[train.id].train = train
train.schedule = new_schedule
script.raise_event(on_trains_updated_event,
{
  trains = global.trains
})
What happens is when assigning the schedule mods subscribed to on_train_schedule_changed will run before I can raise the event. One of those might do something like teleporting (deleting and recreating) the train on a different surface.
So to me your comment means that before every custom event we have to check validity of all objects because we can't be sure no other mod used base events to cut in line and invalidate my globals!?

User avatar
Harag
Inserter
Inserter
Posts: 20
Joined: Sun Sep 30, 2018 9:49 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Harag »

It is quite hard to debug this situation. There is zero feedback that an event got dropped in transit.

And even checking all entity references before sending the event is no guarantee that all subscribers will receive the event. One of the subscribers might invalidate an entity in the event data which will prevent the event from being delivered to all following subscribers.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Pi-C »

Optera wrote:
Sun Apr 02, 2023 5:34 am
Let's say my mod does something like this:

Code: Select all

global.trains[train.id].train = train
train.schedule = new_schedule
script.raise_event(on_trains_updated_event,
{
  trains = global.trains
})
What happens is when assigning the schedule mods subscribed to on_train_schedule_changed will run before I can raise the event.
Harag wrote:
Sun Apr 02, 2023 10:14 am
It is quite hard to debug this situation. There is zero feedback that an event got dropped in transit.
For debugging purposes, I usually listen to all events that I've raised myself, just to log the event data. Thus, I'd add handlers for both on_train_schedule_changed and on_trains_updated_event for the example code above.

If possible, I also try to reproduce bugs with saved games I've got for debugging purposes from players who use a ton of mods. Of course, there's no guarantee that a mod will always work even if it works with that particular saved game! But adding my mod to a game with >100 mods results in a much higher chance to find potential problems that would never occur under lab conditions, with just the minimum set of mods that's required to run my own mod.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Optera »

Pi-C wrote:
Sun Apr 02, 2023 2:03 pm
For debugging purposes, I usually listen to all events that I've raised myself, just to log the event data. Thus, I'd add handlers for both on_train_schedule_changed and on_trains_updated_event for the example code above.
This will only test for mods running before yours.
A mod after yours can invalidate and break the event for all following.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Pi-C »

Optera wrote:
Mon Apr 03, 2023 6:38 am
Pi-C wrote:
Sun Apr 02, 2023 2:03 pm
For debugging purposes, I usually listen to all events that I've raised myself, just to log the event data. Thus, I'd add handlers for both on_train_schedule_changed and on_trains_updated_event for the example code above.
This will only test for mods running before yours.
A mod after yours can invalidate and break the event for all following.
Sorry, perhaps I didn't understand
What happens is when assigning the schedule mods subscribed to on_train_schedule_changed will run before I can raise the event.
correctly. My assumption was that changing train.schedule would raise on_train_schedule_changed (similar to LuaEntity.teleport or LuaControl.driving), and that you would manually raise a custom event after changing train.schedule. I'm not so sure about this anymore because the description of LuaTrain.schedule doesn't have a "Raised events" section -- so either I've misinterpreted your sentence or "Raised events" is missing from the documentation.

Anyway, my post was meant to be a reply to Harag's statement
It is quite hard to debug this situation. There is zero feedback that an event got dropped in transit.
If you know that your action will raise an event (you could write to the log file before changing train.schedule), and if you have registered a handler for logging event data of that event, and if your log file doesn't show that your handler has been run by the time you manually raise the custom event, then this zero feedback actually indicates that your "event got dropped in transit" and you can investigate further.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Harag
Inserter
Inserter
Posts: 20
Joined: Sun Sep 30, 2018 9:49 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Harag »

Pi-C wrote:
Mon Apr 03, 2023 8:03 am
Anyway, my post was meant to be a reply to Harag's statement
What I meant with "debug" is the act of realizing that this is the problem. The engine gives no clue that it disrupted communication and both the sender and the receiver won't fail because neither recognizes that something is wrong. Attaching additional event listeners to verify one's suspicion is the easy part that comes after.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: [boskid] LuaBootstrap::raise_event silently fails for certain custom event data structures

Post by Pi-C »

Harag wrote:
Mon Apr 03, 2023 1:27 pm
What I meant with "debug" is the act of realizing that this is the problem. … Attaching additional event listeners to verify one's suspicion is the easy part that comes after.
OK, never mind -- it's a case of different workflows (logging as much as possible right from the start in order to notice strange things vs. having suspicions first and confirming them by logging later on). Sorry for the noise! :mrgreen:
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Not a bug”