Last week I decided to start blogging about making Umbraco packages and I am going to try to do that every week (except next week because of vacation! <insert happy emoji). Thanks to everyone that decided to read my rambling in the first blog. If you haven't yet, you can read it here.
Today I'll be talking about something that has plagued me last week. It's one of the worst things. Just imagine being all motivated due to the latest blog past and BAM, most of the week spend trying to fix a bug. Well, that's exactly what happened last week. So let's get started!
The bug
The bug was a very strange one. It didn't happen all the time and the error would look like this:
I wasn't quite sure why this was happening. In all my testing on v8, I had never encountered this issue. The description in the stack trace was also a bit strange. It's basically telling me that the issue lies in one of three options and maybe it makes sense to someone more advanced in Umbraco, but I really had no idea what was happening! The description does suggest a solution with "ExecutionContext.SuppressFlow" and "ExecutionContext.RestoreFlow", but I had no idea what that would actually do. The last thing I want is that my package creates errors on an actual live website, so I tend to not include code that I know nothing about. There also wasn't a lot of documentation around what it actually did, so I moved on.
Eventually, I was able to kind of find the source of the bug. I noticed that this bug would only occur if I use my create and start audit function however, the function that only creates the audit and doesn't automatically run the audit didn't have this issue. And the start audit does indeed run in a separate task, so that must be the issue like the stack trace described!
if (postModel.StartAudit)
{
Task.Run(() =>
{
try
{
var result = _siteAuditService.StartSiteAudit(model).Result;
}
catch (Exception ex)
{
_logger.LogError(ex, "Something went wrong!");
}
});
}
So, now that I had figured out where the bug lied, I just had to fix it. Ehmm, yes. But how!? I still wasn't quite sure why this was an issue. It had something to do with the scoping in my code, so I had moved it around a bit to be more in line with the Umbraco variant, but that didn't quite fix the issue.
I thought that maybe it was related to not disposing of one of the scopes correctly, but all of my scopes were disposed of correctly. I thought that maybe there were multiple instances, but after putting everything as a singleton, I still had the issue. I could just feel my motivation slipping away with every "fix" I tried. There was one fix in the back of my mind, but I really wasn't that happy with it.
In the end, I did decide to give up and do my final fix. Instead of calling the start code straight away, I ended up adding the audit to a list so it could be picked up by a task that runs every minute. It maybe isn't the best solution, but it works! Maybe I'll revisit it in the future but for now, I'll stick to making some real progress to my package.
I think every package developer will have such a bug once in their package. A bug that just doesn't want to be fixed. Something that feels like you don't have the knowledge to fix or just don't have the time to figure it all out. After all, making packages is for most people just something to do on the side. Luckily there is usually a way around it, maybe not the cleanest way, but just a workaround. And if that does the job, then who is there really to complain?
I had hoped that I would have been able to show more on the site audit part, but that'll be something in the next two weeks. The package is slowly coming to a v1 and there are so many more things to do after that!
Have a great week everyone!