{"record":{"id":"2f07af4f0241b618","repo":"dotnet/runtime","slug":"unhandled-case-for-marshalaction-d-n","errorCode":null,"errorMessage":"Unhandled case for MarshalAction: %d\\n","messagePattern":"Unhandled case for MarshalAction: (.+?)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/mono/mono/component/marshal-ilgen.c","lineNumber":2107,"sourceCode":"\tcase MARSHAL_ACTION_CONV_RESULT: {\n\t\tchar *msg = g_strdup (\"HandleRefs can not be returned from unmanaged code (or passed by ref)\");\n\t\tcb_to_mono->methodBuilder.emit_exception_marshal_directive (mb, msg);\n\t\tbreak;\n\t}\n\n\tcase MARSHAL_ACTION_MANAGED_CONV_IN:\n\t\tfprintf (stderr, \"mono/marshal: SafeHandles missing MANAGED_CONV_IN\\n\");\n\t\tbreak;\n\n\tcase MARSHAL_ACTION_MANAGED_CONV_OUT:\n\t\tfprintf (stderr, \"mono/marshal: SafeHandles missing MANAGED_CONV_OUT\\n\");\n\t\tbreak;\n\n\tcase MARSHAL_ACTION_MANAGED_CONV_RESULT:\n\t\tfprintf (stderr, \"mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\\n\");\n\t\tbreak;\n\tdefault:\n\t\tfprintf (stderr, \"Unhandled case for MarshalAction: %d\\n\", action);\n\t}\n\treturn conv_arg;\n}\n\nstatic int\nemit_marshal_object_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,\n\t\t\tMonoMarshalSpec *spec,\n\t\t\tint conv_arg, MonoType **conv_arg_type,\n\t\t\tMarshalAction action)\n{\n\tMonoMethodBuilder *mb = m->mb;\n\tMonoClass *klass = mono_class_from_mono_type_internal (t);\n\tint pos, pos2, loc;\n\n\tMonoType *int_type = cb_to_mono->get_int_type ();\n\tswitch (action) {\n\tcase MARSHAL_ACTION_CONV_IN:\n\t\t*conv_arg_type = int_type;","sourceCodeStart":2089,"sourceCodeEnd":2125,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/mono/component/marshal-ilgen.c#L2089-L2125","documentation":"In emit_marshal_handleref_ilgen the switch over MarshalAction falls through to default when the action value is not one of the known MARSHAL_ACTION_* constants. The marshaller prints 'Unhandled case for MarshalAction: <n>' to stderr and returns conv_arg unchanged, i.e. it emits nothing for an action it does not recognize. This is effectively an internal-consistency failure: a new enum member was added to the runtime without a matching case here.","triggerScenarios":"A new MarshalAction enum value is introduced in the Mono runtime and the marshaller is invoked for a HandleRef with that action before this switch is updated. Should never happen for a released enum set; indicates an in-source inconsistency.","commonSituations":"In-tree development: adding a MarshalAction enum member and exercising HandleRef marshalling before updating emit_marshal_handleref_ilgen; merging a runtime change without the matching marshaller update.","solutions":["Add the missing case to emit_marshal_handleref_ilgen for the new MarshalAction value and emit the correct IL (mirror the SafeHandle/object handlers).","If seen unexpectedly on a stock runtime, report it as a Mono runtime bug: the switch must cover every MarshalAction member.","Ensure MarshalAction enum and all emit_marshal_*_ilgen switches are updated together in the same change."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// In-tree: assert every MarshalAction enum member is handled by each emit_marshal_* switch.\nstatic void AssertMarshalActionsCovered() {\n    foreach (MarshalAction a in Enum.GetValues(typeof(MarshalAction)))\n        if (!HandledActions.Contains(a))\n            throw new InvalidOperationException($\"emit_marshal_handleref_ilgen missing case for {a}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When adding a MarshalAction enum member, update every emit_marshal_*_ilgen switch in the same change.","Add a unit test that enumerates MarshalAction against each switch to prevent regressions.","Treat seeing this message on a stock runtime as a bug to report upstream.","Code-review interop marshaller changes for switch completeness."],"tags":["mono","marshal","handleref","ilgen","internal","enum","unhandled-case"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}