davedotluebke / old-skool-text-game

Coding project used to learn Python
5 stars 4 forks source link

Bad code (infinite loops, memory leaks) shoulnd't crash the game #147

Open davedotluebke opened 4 years ago

davedotluebke commented 4 years ago

It seems to me that we can use asyncio and waiting primitives to be sure that any wizard code cannot halt the game by going into an infinite loop:

https://docs.python.org/3/library/asyncio-task.html#waiting-primitives

Essentially you get to launch a task into the event loop (and the task can itself launch more tasks if it wants) and specify a timeout in seconds; after the timeout period the function returns a tuple with the tasks that are done and the tasks that are still pending. Presumably we could just drop any tasks that take too long on the floor, and never complete them.

@1Bayshore thinks this is likely to require us to decorate or label as async every single function in the game, which would be a huge hassle, but we need to investigate more.

This wouldn't fully prevent rogue or incompetent wizards from crashing the game or slowing it to a crawl, because they could fairly easily write code that spawns a zillion objects and takes up all of memory (or enough to make the system start thrashing), or spawns a zillion more tasks and overwhelms the event loop, but it would help a lot with the most common coding mistakes that a less experienced wizard is likely to make (or sometimes even a very experienced wizard!)

We would also like to detect and prevent memory leaks somehow - no ideas on this yet.

rivques commented 4 years ago

We may end up going through the game to decorate adminonly functions, so this could happen when that does.