Jessecar96 / SteamBot

Automated bot software for interacting with Steam Trade
http://scrap.tf
MIT License
1.33k stars 911 forks source link

Occasionally fetching the schema fails and causes the bot to crash. #399

Open waylaidwanderer opened 11 years ago

waylaidwanderer commented 11 years ago

Here's what happens:

[Happy Bot 2013-07-23 17:10:06] ERROR: Unhandled exceptions in bot Happy Bot callback thread: 
 System.NullReferenceException: Object reference not set to an instance of an object
  at SteamTrade.Schema.FetchSchema (System.String apiKey) [0x00000] in <filename unknown>:0 
  at SteamBot.Bot.<HandleSteamMessage>b__77 (SteamKit2.LoginKeyCallback callback) [0x00000] in <filename unknown>:0 
  at SteamKit2.CallbackMsg.Handle[LoginKeyCallback] (System.Action`1 handler) [0x00000] in <filename unknown>:0 
  at SteamBot.Bot.HandleSteamMessage (SteamKit2.CallbackMsg msg) [0x00000] in <filename unknown>:0 
  at SteamBot.Bot.BackgroundWorkerOnDoWork (System.Object sender, System.ComponentModel.DoWorkEventArgs doWorkEventArgs) [0x00000] in <filename unknown>:0 
  at System.ComponentModel.BackgroundWorker.OnDoWork (System.ComponentModel.DoWorkEventArgs e) [0x00000] in <filename unknown>:0 
  at System.ComponentModel.BackgroundWorker.ProcessWorker (System.Object argument, System.ComponentModel.AsyncOperation async, System.Threading.SendOrPostCallback callback) [0x00000] in <filename unknown>:0 

Is there any way to avoid this? Perhaps by throwing a try-catch around it? That would probably work as an interim method, but perhaps there's a more elegant solution.

Jessecar96 commented 11 years ago

Here's what I did for my bots. Might not be the best, but it works. I modified my bots to only get one schema in Program.cs and share it so this probably won't work on the real SteamBot, but you get the concept of it.

    if (MasterSchema == null)
    {
        MasterSchema = null;
        Console.WriteLine("Downloading schema...");
        while (MasterSchema == null)
        {
            try
            {
                MasterSchema = Schema.FetchSchema(configObject.ApiKey);
            }
            catch (Exception)
            {
                MasterSchema = null;
                Console.WriteLine("Problem while getting the schema.  Retrying in 10 seconds...");
                Thread.Sleep(10000);
            }
        }
        Console.WriteLine("Downloaded schema!");
    }
Jessecar96 commented 11 years ago

It will work, you just have to change the variable names because I changed some for the ScrapTF version.

geel9 commented 11 years ago

Jesse you need some mutexes or locks up in this bitch

waylaidwanderer commented 11 years ago

The current SteamBot does have a mutex for downloading the schema. Adding in a retry as @Jessecar96 suggested sounds good though.

geel9 commented 11 years ago

Last I checked you guys were trying some file lock shit

cwhelchel commented 11 years ago

@geel9 It's a mutex. The lock file mess didn't work IIRC.

waylaidwanderer commented 10 years ago

I haven't run into this error for a while now.

cwhelchel commented 10 years ago

I thought it was resolved. Perhaps it needs to be more robust in general?

StormReaper commented 10 years ago

I changed the behaviour of the schema fetching to use the modified since header and if there's any issues with the API it loads the local schema if present.

https://github.com/StormReaper/SteamBot/commit/4c6dd1f8144e66ac3bb3e7a479fe10743b797cb8

Cyb3r16 commented 9 years ago

This issue is mainly present with CSGO. Those api's are offline every other day. @Jessecar96 that fixed it, thanks.

einsteinsci commented 8 years ago

You could make it so that the Schema retrieves the last cache whenever it encounters a WebException, by changing Schema.FetchSchema() and Schema.GetSchemaString(). That way, it will only crash if there is no schema file cached when the schema server is down.

Pastebin link to those two methods

I can't submit a PR as my fork of this repository is just for personal use (and tweaks). Could somebody test the changes above to see if they work on their own?