Tencent / rapidjson

A fast JSON parser/generator for C++ with both SAX/DOM style API
http://rapidjson.org/
Other
14.25k stars 3.53k forks source link

how to avoid rapidjson to crashing on exeption... #1713

Closed Alphadan28 closed 2 years ago

Alphadan28 commented 4 years ago

im getting this exeption:

BotSupp: rapidjson/document.h:1218: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator]: Assertion `false' failed.

im building a bot which processes the json produced by another program and depending on those values it will send me alerts to telegram.

the problem happens once a day or so, so is a race condition, i guessing its caused by opening a incomplete json but some how not producing parse error.

Tried to enclose the code with a try catch but still crashes.

this is the code:

try {

         FileReadStream is(pFile, buffer, lSize);

         Document d;
         d.ParseStream(is);
         if(!d.HasParseError()){

             double PEntry=ReadDbl(d,"priceToBuy");
             double PExit=ReadDbl(d,"priceToSell");
             double lowBB=ReadDbl(d,"lowBB");
             double highBB=ReadDbl(d,"highBB");
             double ema1=ReadDbl(d,"ema1");
             double ema2=ReadDbl(d,"ema2");
             double rsi=ReadDbl(d,"rsi");
             double PBid=ReadDbl(d,"bid1");
             double PAsk=ReadDbl(d,"ask1");

             double PLB=ReadDbl(d,"latestBuyRate");
             double PLS=ReadDbl(d,"latestSellRate");
             //Balance
             double Blns=ReadDbl(d,"baseBalance");
             double Orders=ReadDbl(d,"onOrdersBalance");

             char B[1000];
             sprintf(B,"P1=%.2f&P2=%.2f&P3=%.2f&P4=%.2f&P5=%.2f&P6=%.2f&P7=%.2f&P8=%.2f&P9=%.2f&P10=%.2f&P11=%.2f&P12=%.2f&P13=%s&P14=%.2f&P15=%.2f", CurrPrice, PEntry, PExit, lowBB, highBB, ema1, ema2, rsi, PBid, PAsk, PLB, PLS, PeriodInf, Blns, Orders);
             char R[200];
             size_t RS=200;

             HttpRequestPost("http://127.0.0.1/store.php",B,R,RS);
         }

} catch (fs::filesystem_error &e) {
  cout << "Standard exception: " << e.what() << endl;
}   
double ReadDbl(Document &d,const char* key){
    try{
       if (d[key].IsDouble()){
           return d[key].GetDouble();
       }else{
           if(d[key].IsString()){
              std::string tv = d[key].GetString();
              return atof(tv.c_str());
            }

        }
    }catch (exception& e){
       cout << "Standard exception: " << e.what() << endl;
    }
    return 0;
}
miloyip commented 4 years ago

Is it possible that the key does not exist? For non-optimal performance, try:

if (d.HasMember(key) && d[key].IsDouble())
Alphadan28 commented 4 years ago

thanks will try it .

This checks if the key exist before trying to get the value right?.

I thought about doing this but i was trying a more efficient solution like handling the exception since is a race condition.

The key should be there always the thing is that when i read the file the other program might be writing it in that exact moment and may be, there is a small chance to read the json incomplete in most of the cases the if(!d.HasParseError()){ would ignore it and try to read it 5s after.

Im using a raspberry pi so my resources are limited is there any way to just handle the exception avoiding it to crash instead of make an additional search for each key? the json is quite big around 580kb.

Alphadan28 commented 4 years ago

your solution seems to work is about 24 hours running without crashing.

Alphadan28 commented 4 years ago

crashed again :(

Alphadan28 commented 2 years ago

The library is working perfectly I forgot to report here, the issue was never rapidjson,

My project opens a file periodically, which is created by another process and sometimes the file was open before the other process completed the saving of the file so the error came when opening partial JSON.

The solution was to just ensure that the file was completed before trying to parse.

So far this project is running continuously for more than a year and has not crashed even a single time.

you can close this as i got the solution and THANKS for such amazing library.