Custom timeout: is there a way to know which line of code was being executed?

北城余情 提交于 2019-12-25 12:54:37

问题


(which line of code is being executed in a binary in production, not in Visual Studio)

First a bit of framework for my problem: I have an error that is very difficult to track, because it occurs randomly. In this error, Dapper hangs indefinitely. No timeout exception, no nothing. Just one query that executes indefinitely. Probably a "nasty deadlock case", as Marc Gravell answered in the comments.

In order to narrow down where the problem comes from, I created this timeout function:

/// <summary>
/// Usage:
/// var myResult = GetResultWithTimeout(() => MyFunction());
/// </summary>
public static TResult GetResultWithTimeout<TResult>(Func<TResult> func)
{
    var task = Task.Run(func);
    if (task.Wait(TimeSpan.FromSeconds(TimeoutInSeconds)))
        return task.Result;
    else
        throw new Exception($"Function timed out: {func.ToString()}");
}

I plan to wrap all my _db.Query and _db.Execute (quite a few) with this function. I am not able to track the error when debugging, because it occurs once every 1000(?) executions without a visible pattern. This is why I need to add this wrapper to my code in production, and hope that the additional info that I am catching helps me track down the problem.

Now, it would be much better to know which exact operation is Dapper locked in, hence the title of the question: is there a way to know which line of code is being executed when the timeout occurs? If I could log this info in my exception, this would give me the info about where the deadlock occurs in Dapper.

来源:https://stackoverflow.com/questions/40017402/custom-timeout-is-there-a-way-to-know-which-line-of-code-was-being-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!