Skip to content

crash when Reading waypoints #443

Closed
Closed

Description

@yakra

HighwayData @ 2661ba63dcd717d679c5ce8f6bc8c1f94103c34b
UserData @ cdaaf1365cfb7181792fa906619962143da6ffc5
DataProcessing @ 5ce722a

Happened during usapr, though that probably doesn't matter much.

Activity

  1. yakra commented on Jun 26, 2021

    @yakra
    ContributorAuthor

    Route::read_wpt: Comment out...

    • Individually commenting out the 15 single-point Datachecks + the HighwaySegment ctor = crashes continue.
    • per-route datachecks = crashes continue.
    • label_too_long = crashes continue.
    • WaypointQuadtree::insert = crashes continue.
    • std::cout << '.' << std::flush; <--There's no mutex around this. All threads can just go crazy & do whatever.
      Still crashes! :(
    • anything else?

    Verbosity

    • Crashes when reading usaca are very common. Add debug text; see if it hangs on a specific route.
      Could also be whatever's processed concurrently. Need to know what systems are in flight at any given time.
      • create static std::vector<HighwaySystem*> HighwaySystem::in_flight(Args::numthreads,0); and have ReadWptThread write to & display it.
  2. yakra commented on Jun 27, 2021

    @yakra
    Author
  3. yakra commented on Jun 28, 2021

    @yakra
    ContributorAuthor

    LOL disregard that last post.
    I ran my new code without compiling it first. :P

    Workaround to iterate thru the systems list without a mutex:
    Tried it anyway. No luck. Still crashes.

    Now what?
    Gotta run 2 threads and observe what both are doing when the crash occurs, without producing an overly huge log file in the process.
    This means filtering debug text for the 2 systems I expect to be in progress at the time.
    It's usually usaal & usaca, so I'm filtering for that. This last time with the new code, got usafl & usaca.
    Gonna keep going till I get usaal & usaca, and see what I can see.
    Better yet, set up variants to capture both combos, and see which brings results first.
    Even betterer,, set up one to capture all 3. The log file will be bigger, but not huge.

  4. jteresco commented on Jun 28, 2021

    @jteresco
    Contributor

    One of the most frustrating things in my long-ago experiences debugging parallel code is that the fact you put in logging or write files to track down problems can change the timing and cause problems that weren't evident originally as well as mask problems you might have seen previously so they look like they're fixed...until you remove the debug code.

  5. yakra commented on Jun 29, 2021

    @yakra
    ContributorAuthor

    Taking 2 different approaches in parallel:

    1. Add verbosity to everything that happens during Route::read_wpt, to see what both threads are doing at the time of the crash, and if there's something consistent. This may not be the most helpful if the effects of whatever goes amok aren't immediate.
    2. Keep on deleting code until the crashes stop.

    Dead branch: debug @ b31046b78fa373a5e5f028705ae1a6f4481b0ddc superseded by 36abf08

  6. yakra commented on Jun 30, 2021

    @yakra
    ContributorAuthor
    1. Add verbosity to everything that happens during Route::read_wpt, to see what both threads are doing at the time of the crash, and if there's something consistent. This may not be the most helpful if the effects of whatever goes amok aren't immediate.

    I don't see anything consistent. This approach is not helpful.

    1. Keep on deleting code until the crashes stop.

    Doing this. Still though, need to go down 2 rabbit holes in parallel:
    #443 (comment):

    WaypointQuadtree::insert = crashes continue.

    Crashes continue in the DataProcessing variations where I'm adding debug text to the existing code.
    In TMMinimal however, after removing loads of other code, removing WaypointQuadtree::insert stops the crashes.

    So there are at least 2 causes of crashes:

    1. WaypointQuadtree::insert (what part of it?)
      It's not a std::recursive_mutex problem; reverting ef1894f did not help.
    2. Something else, TBD
  7. yakra commented on Jul 1, 2021

    @yakra
    ContributorAuthor

    In TMMinimal however, after removing loads of other code, removing WaypointQuadtree::insert stops the crashes.

    Or not. It could be that it just made them a lot less frequent.
    TMMinimal2 first removes a ton of code, but not as much as TMMinimal. Testing out removing WaypointQuadtree::insert from a range of commits, some ran 430, 1009, 437, 1952 or 2033 times before crashing. One of them is still going @ 5007, and expected to crash eventually.

    So there are at least 2 causes of crashes:

    Could be 2 causes, could be just 1. It may be that our Something else TBD throws the ball, and WaypointQuadtree::insert is somehow better able to catch it.

    This will take a lot of patience. If i'm not concerned with poring thru siteupdate.log anymore, hopefully >2 threads can bring on the crashes quicker. If I end up needing too many threads, I'll use lab3 rather than noreaster.

    To be continued...

  8. yakra commented on Jul 2, 2021

    @yakra
    ContributorAuthor

    Grawlix.

    Have I linked to http://www.catb.org/jargon/html/H/heisenbug.html yet? If not, doing so now.
    (Edit: Yes.)

    TMMinimal2@d646f31 crashed on noreaster, after 2051 passes. That's good. I want to see that.

    And then there's its child commit, which cuts out some unused code.
    At this point in TMMinimal2, no TravelerList objects are ever created. The few non-static members left in the class are just there because they're referenced by some other part of the code (Route, and... HighwaySegment?) I'd yet to snip out.
    TravelerList is only being kept around for its 2 remaining static members:

    • Traverlist::mtx, which has been repurposed as a terminal mutex for keeping the debug text readable.
    • Travelerlist::ids, analogous to traveler_ids in siteupdate.py. The list of traveler ids is still populated in main() and takes up some memory, altering however subtly the operating environment once we start reading wpts.

    I ran this code, and 3 of its child commits, in a loop via untilcrash.sh, approx 5100 times. 4 threads on noreaster, 6 on my BSD 13.0 machine.
    So, what's different?

    • A few static members of Travelerlist are gone, that took up a whopping 112 B.
    • 5 functions are gone, including the TravelerList ctor. Even if they weren't called, these instructions all took up however much memory. Right?

    It'd appear that getting this stuff out of memory changes where everything else sits in memory afterward, getting it out of the "danger zone". Our pointers there don't get mangled, and we don't observe the bug.
    This doesn't solve the problem; it only masks the symptom.
    I should be able to remove any number of things from TMMinimal2@d646f31, doesn't particularly matter what, and similarly sweep the problem under the rug.

    This has to be a bug in BSD clang.

    • Tens of thousands of runs built with clang G++ on Linux & I've not seen these crashes once.
    • Built with clang... Hm. Maybe only about 1000? Time to run untilcrash.sh (f8c262e) on lab1 & lab2 to be absolutely sure...

    It'd be nice to be able to submit a bug report to the developers, if I can ever isolate it. Of course, this means a minimal version of the program with only what's necessary to observe the crash. No good pointing them toward 7500+ lines of siteupdate and saying Have fun finding the bug!
    I fear that I'll only sweep the problem under the rug before isolating it...

    What next?

    • Maybe go back to the first TMMinimal (somewhere around ac6eaf) and leave WaypointQuadtree::insert in, making the crashes easier to observe, more frequent. Continue removing code, making sure it still crashes as long as it can.

    • crash when Sorting waypoints in Quadtree #442 (comment)

      Will probably also test out 11.4 for giggles.

      Have yet to do this. With any luck, the bug was introduced to the compiler between FreeBSD 11.4 & 12.2.

  9. yakra commented on Jul 2, 2021

    @yakra
    ContributorAuthor

    #443 (comment)

    In TMMinimal however, after removing loads of other code, removing WaypointQuadtree::insert stops the crashes.

    Or not. It could be that it just made them a lot less frequent.

    A hell of a lot less frequent.

    TMMinimal2 first removes a ton of code, but not as much as TMMinimal. Testing out removing WaypointQuadtree::insert from a range of commits, some ran 430, 1009, 437, 1952 or 2033 times before crashing. One of them is still going @ 5007, and expected to crash eventually.

    2 minutes after my last post, it finally crashed. After 10056 passes.

    This will take a lot of patience.

    And so there's an indication of just how much patience...


    #443 (comment)

    Maybe go back to the first TMMinimal (somewhere around ac6eaf) and leave WaypointQuadtree::insert in, making the crashes easier to observe, more frequent. Continue removing code, making sure it still crashes as long as it can.

    For the sake of sanity, expedience, and the ability to observe crashes with any kind of regularity, this looks like a better option than continuing down the TMMinimal2 road.
    Edit: Tracking progress here. Thankfully, the crashes are happening really quickly. For now at least...

  10. yakra commented on Jul 3, 2021

    @yakra
    ContributorAuthor

    TMMinimal

    Progress has been steady.
    Everything after the Reading waypoints for all routes loop has been removed except for the Total run time text, in order to keep untilcrash.sh working.
    Everything before it has been removed, except what's necessary to set it up.
    The total line count of all .h + .cpp files is down to 731 from 7505.

    Waypoint class

    Unnecessary variables removed from all classes except Waypoint. Changes to sizeof(Waypoint) could have big effects on where everything sits in memory, and possibly mask the problem. Best be cautious here.
    Incidentally, sizeof(Waypoint) differs between FreeBSD & Ubuntu when compiled with clang (haven't looked at CentOS, and I wanna say g++ is the same on Ubuntu). Padding out the Waypoint structure to 192 bytes = it still crashes, unsurprsingly.

    Now the real fun begins...

    ...hacking away at parts of the loop itself, seeing what crashes and what doesn't.
    First thing to go will be the set of all .wpt filenames in hwy_data/ used for tracking unprocessedwpts. I can cut out a good amount of code with minimal impact; early tests indicate I'll still see crashes.

  11. yakra commented on Jul 4, 2021

    @yakra
    ContributorAuthor
    • TMMinimal@bdf9590 crashed, even if it took 13191 passes to do so.
      This means losing WaypointQuadtree = we still get crashes.
    • TMMinimal@d18a904
      This means losing everything but WaypointQuadtree and what's absolutely necessary to keep the ReadWptThread loop running = we still get crashes.

    This suggests that whatever's happening is during the barebones setup & ReadWptThread loop, and that removing WaypointQuadtree from d18a904 (yieldingTMMinimal@9df3607) would still cause crashes, even if it takes thousands and thousands of passes to observe them.
    Just started testing on noreaster:

    • siteupdate -t 4
    • siteupdate -t 1
    • siteupdateST
      • Thinking back to C++ site update to production #375 (comment), remember that even siteupdateST crashed. We're seeing the same... (is it a cause or a symptom?) now, of a pointer's value mysteriously decreasing by 0x900000000. Single-threaded crashes could still be a thing, if I'm patient & hang around long enough to observe them.
    • Will reverting d6da523 make crashes more frequent & easier to observe? Maybe, maybe not. Testing 2t, 1t & ST.
    • Or could std::list<Waypoint*> *colocated having been after a bubble of empty space between unsigned int point_num and std::string label have been a factor? Reverting 383f269 & trying that too. 2t, 1t & ST.

    Now to sit back and wait. For... however long it takes.
    If I still see crashes, it might be time to go to https://llvm.org/docs/HowToSubmitABug.html ...
    TMMinimal is down to 437 lines; hopefully that's not too much to wade through. :)


    Still haven't looked at FreeBSD 11.4. Now's a good time...
    Edit: Or maybe try out valgrind...

  12. jteresco commented on Jul 5, 2021

    @jteresco
    Contributor

    In my days of more heavy duty parallel programming, we had some high-end tools available to help find memory errors, including TotalView, which appears to remain in existence: https://totalview.io/products/totalview

    It doesn't look like there's a FreeBSD version, however.

  13. yakra commented on Jul 5, 2021

    @yakra
    ContributorAuthor

    Giving valgrind a go on lab3.

  14. yakra commented on Jul 5, 2021

    @yakra
    ContributorAuthor

    OK. Wow. I think I've got it.
    I'll post more when I can put a sentence together.
    First, sleep.

  15. jteresco commented on Jul 5, 2021

    @jteresco
    Contributor

    Good! Awaiting the big reveal of what you've found out!

  16. yakra commented on Jul 5, 2021

    @yakra
    ContributorAuthor

    #443 (comment)

    Crashes when reading usaca are very common. Add debug text; see if it hangs on a specific route.

    Oh man. I came so close. It's difficult to identify a specific route via siteupdate.log because we may be a few routes farther along in the process before the crash happens (and I was focusing on what happens at the Waypoint level & missing the bigger picture).
    I opened ca.ca061pos in the HB. May have even thrown it into GHex at one point. Must not have done a text editor though, or I would have seen the blank line at the beginning of the file.
    There are 2 other offending files -- rou.dk142j, and it was pse.h060 that finally tipped me off.

    A .wpt file is first slurped into core as big-ol' char array. And then,

    // split file into lines
    size_t spn = 0;
    for (char* c = wptdata; *c; c += spn)
    { for (spn = strcspn(c, "\n\r"); c[spn] == '\n' || c[spn] == '\r'; spn++) c[spn] = 0;
    lines.emplace_back(c);
    }

    Paraphrasing for expository purposes, with while instead of for:

    // split file into lines				// When 1st line is blank
    size_t spn = 0;						// Here's what happens:
    char* c = wptdata;					// ----------------------
    while (*c)						// *c == '\n'; proceed
    {	spn = strcspn(c, "\n\r");			// spn = 0
    	while (c[spn] == '\n' || c[spn] == '\r')	// c[0] == '\n'; proceed
    	{	c[spn] = 0;
    		spn++;					// spn = 1
    	}
    	lines.emplace_back(c);				// lines[0] = wptdata
    	c += spn;					// c = wptdata+1
    }

    The loop repeats starting at wptdata+1 -- c is the start of the first waypoint label, and lines[1] becomes wptdata+1. SFSG.

    The trouble starts when we go to strip whitespace from the end of lines[0]:

    char * endchar = lines[l+1]-2; // -2 skips over the 0 inserted while splitting wptdata into lines

    • lines[1] is wptdata+1.
    • Thus endchar is wptdata-1.
    BLAMMO! A pointer to one byte before the beginning of the file in memory!
    Now let's say we have a memory address stored there, like 0x912e2a0e0.
    Let's put these 8 bytes in little-endian order: e0 a0 e2 12 09 00 00 00.
    Next line,

    while (*endchar == 0) endchar--; // skip back more for CRLF cases, and lines followed by blank lines

    The incorrect assumption is that we're in the middle of the file somewhere.
    This skips us back past the three 00s, putting us at the 09. Which is ascii TAB...

    while (*endchar == ' ' || *endchar == '\t')
    { *endchar = 0;
    endchar--;
    }

    ...and gets zeroed out.
    And we proceed back through memory, to potentially do more damage. This explains the couplefew pointer values I saw with a high order 0x920 zeroed out; 0x20 is ascii Space.

    Why did I never see this on Linux?
    #375 (comment)

    (Worth noting that the g++ version doesn't store Waypoints in this memory range -- more 0x84####### ballpark. Could this be why it doesn't crash?)

    Yup. For clang on Linux, either this is still the case, with pointers stored out of 0x9######## range, or I just never happened to observe the problem in the moderate number of runs I did with clang.

    Anyway!
    Now that the problem is finally identified, fixing it will be very easy.
    I see a couple ways to do it; just need to decide which is better.

  17. yakra commented on Jul 6, 2021

    @yakra
    ContributorAuthor

    So I was like "O HAY, the TravelerList constructor processes lines the same way; that'll need a fix too!"
    Oh look. It already got one a year ago.
    If only I'd made the same connection back then... 🤣

  18. added a commit that references this issue on Jul 6, 2021
    e90d92c
  19. jteresco commented on Jul 6, 2021

    @jteresco
    Contributor

    And it look like the Python version doesn't care about a blank line because it breaks the file into lines first, strips off whitespace, and ignores any line that's empty after that.

  20. yakra commented on Jul 6, 2021

    @yakra
    ContributorAuthor

    Yup. C++ does the same thing too -- just not 100% successfully till now. :)

    The relevant part of the diff is here.

    And as for how it does the splitting into lines & stripping whitespace, well...
    This kind of madness is the reason some languages don't do pointer arithmetic. :D

  21. jteresco commented on Jul 6, 2021

    @jteresco
    Contributor

    I feel like my C class in the fall and my Programming Languages class whenever I can offer that next need to use this bug as a case study for the dangers of a language trusting the programmer too much.

  22. yakra commented on Jul 7, 2021

    @yakra
    ContributorAuthor

    It made me start looking into Rust.

    Meanwhile, as the results of valgrind are starting to come in, see I'm doing similar stuff elsewhere, even if not with pointers.

    // read in fp file
    unordered_set<string> nmpfps;
    file.open(Args::highwaydatapath+"/nmpfps.log");
    while (getline(file, line))
    { while (line.back() == 0x0D || line.back() == ' ') line.erase(line.end()-1); // trim DOS newlines & whitespace
    if (line.size()) nmpfps.insert(line);
    }
    file.close();

    Quote from http://cplusplus.com/reference/string/string/back/

    This function shall not be called on empty strings.

    Ha. Guess what I'm doing.
    I've fixed stuff like this before...
    How come I've never seen a crash like in #402 when reading nmpfps.log? It must depend on what's in memory before line.data()...

  23. yakra commented on Jul 8, 2021

    @yakra
    ContributorAuthor

    #443 (comment)

    So I was like "O HAY, the TravelerList constructor processes lines the same way; that'll need a fix too!"
    Oh look. It already got one a year ago.

    Still needs a fix, actually.

    char * endchar = lines[l+1]-2; // -2 skips over the 0 inserted while separating listdata into lines

    can still result in endchar being initialized to one byte before the beginning of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions