{"record":{"id":"818163b588734cf9","repo":"dotnet/BenchmarkDotNet","slug":"async-void-is-not-supported-by-design-818163","errorCode":null,"errorMessage":"Async void is not supported by design.","messagePattern":"Async void is not supported by design\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Toolchains/InProcess/NoEmit/BenchmarkActionFactory.cs","lineNumber":122,"sourceCode":"        return Create(\n            typeof(BenchmarkAction<>).MakeGenericType(resultType),\n            resultInstance,\n            targetMethod,\n            unrollFactor);\n    }\n\n    private static void PrepareInstanceAndResultType(object instance, MethodInfo targetMethod, out object? resultInstance, out Type resultType)\n    {\n        resultInstance = targetMethod.IsStatic ? null : instance;\n        resultType = targetMethod.ReturnType;\n\n        if (resultType == typeof(void))\n        {\n            // DONTTOUCH: async should be checked for target method\n            // as fallbackIdleSignature used for result type detection only.\n            bool isUsingAsyncKeyword = targetMethod?.HasAttribute<AsyncStateMachineAttribute>() ?? false;\n            if (isUsingAsyncKeyword)\n                throw new NotSupportedException(\"Async void is not supported by design.\");\n        }\n        else if (resultType.IsPointer && resultType != typeof(void*))\n        {\n            throw new NotSupportedException($\"Default {nameof(BenchmarkActionFactory)} only supports void* return, not T*\");\n        }\n    }\n\n    /// <summary>Helper to enforce .ctor signature.</summary>\n    private static BenchmarkActionBase Create(Type actionType, object? instance, MethodInfo method, int unrollFactor) =>\n        (BenchmarkActionBase)Activator.CreateInstance(actionType, instance, method, unrollFactor)!;\n\n    private static readonly MethodInfo FallbackSignature = new Action(BenchmarkActionBase.OverheadStatic).GetMethodInfo();\n\n    public static IBenchmarkAction CreateWorkload(IBenchmarkActionFactory? factory, Descriptor descriptor, object instance, int unrollFactor, bool consumeTasksSynchronously = false) =>\n        CreateCore(factory, instance, descriptor.WorkloadMethod, unrollFactor, consumeTasksSynchronously);\n\n    public static IBenchmarkAction CreateOverhead(IBenchmarkActionFactory? factory, Descriptor descriptor, object instance, int unrollFactor) =>\n        CreateCore(factory, instance, FallbackSignature, unrollFactor);","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Toolchains/InProcess/NoEmit/BenchmarkActionFactory.cs#L104-L140","documentation":"async void methods cannot be awaited or timed correctly, so BenchmarkDotNet rejects them by design; return Task instead.","triggerScenarios":"Thrown at src/BenchmarkDotNet/Toolchains/InProcess/NoEmit/BenchmarkActionFactory.cs:122 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Change the benchmark method signature from 'async void' to 'async Task'.","If the method cannot be changed, wrap the async void call in a synchronous method that blocks on a Task."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}