Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Hang in strange mood code  (Read 1063 times)

0x517A5D

  • Bay Watcher
  • Hex Editor‬‬
    • View Profile
Hang in strange mood code
« on: August 18, 2008, 10:25:30 am »

Last night, DF hung on me.  It became completely unresponsive and was using 100% of one core of my CPU.

There were no indications as to why it hung.

I attached a debugger and did some analysis.

It was stuck in a tight loop with no valid exit condition.

The routine involved is the only one that uses the strings " has been possessed!" and " withdraws from society..." and " is taken by a fey mood!", so it is associated with the strange mood code.

Here's my analysis:

Spoiler (click to show/hide)

So:
Code: [Select]
    some_variable = -1;
    do {
        switch (mt_rand() % 3) {
            case 0:
                some_variable = 13;
                continue;
            case 1:
                some_variable = 14;
                if (a_condition) continue;
            case 2:
                some_variable = 15;
                if (a_different_condition) continue;
        }
    } while (some_variable != -1);

This loop can never exit!  In two ways: if both of the conditions are nonzero (TRUE), the switch will loop forever.  (This occured in my case, and may be related to whatever triggers this loop.)  Even if not, the do/while will loop forever.

The 13, 14, 15 magic numbers are probably enums or symbolic constants.

Also, the condition variables may be two members of a local strucure or object.

This code will be found about 2/3 of the distance between the start and end of the function.

I forced the loop to exit (by setting EIP past the loop) and the game continued normally with the message "Eshtan Dodokzoluth, Peas has been possessed!"

I hope this is useful in tracking down whatever's going on here.
Logged

Toady One

  • The Great
    • View Profile
    • http://www.bay12games.com
Re: Hang in strange mood code
« Reply #1 on: August 18, 2008, 11:00:58 am »

Seems to be the moody glassmaker searching for a type of glass to use, provided they don't like a type of glass.  != was supposed to be == there (just rewrote it without the while to cremate it), but it would be rare enough that it probably accounts for many of the occasional mood-hangs that I couldn't figure out or readily reproduce from saves.  The a_condition/a_different_conditions were whether or not raw clear/crystal glass had been produced in the fort, so without those it wouldn't reproduce when the mood hits the glass-indifferent glassmaker 2/3rds of the time.  I guess that explains the difficulty there.  Thanks for the catch.

I should probably sleep like I wanted to an hour and a half ago...  failure to do so correlates with this sort of thing most likely.
« Last Edit: August 18, 2008, 11:02:35 am by Toady One »
Logged
The Toad, a Natural Resource:  Preserve yours today!