fix(scripting): #1100 a script result is what an OPC UA node can carry, in both runners — Guid crosses the wire, unpublishable types are refused alike
Where an in-process result really ends up: VirtualTagActor -> VirtualTagHostActor -> OpcUaPublishActor ->
OtOpcUaNodeManager.WriteValueLocked -> SdkValueBoundary.FromClr (VariantHelper.TryCastFrom). Probed against
SDK 2.0.1: Guid / Guid[] become an OPC UA Guid scalar/array; KeyValuePair<,>[] (a returned Dictionary),
decimal, enum, TimeSpan, char, DateTimeOffset, tuples, Nullable<T>[] and rank>1 arrays are refused, so the
node write threw, OpcUaPublishActor logged a Warning, and the node kept its PREVIOUS value and status (a
silent freeze, possibly under Good) while the worker answered "unsupported type".
Decision, per type, by what a client can see:
- Guid (scalar + 1-D array): representable -> the wire gains an additive GUID tag (15, `bytes g = 10`,
16 ToByteArray bytes; a wrong length is a protocol fault). Guid INPUTS cross too. protocol_version stays 1:
the worker ships in the host's script-worker/ folder, and an unknown tag already fails closed.
- Everything FromClr refuses: refused identically in both runners by ScriptResultMaterializer, inside the run,
with one reason ("script threw: The script returned a value of type 'KeyValuePair<Int32, Double>[]', which
an OPC UA node cannot carry; return ..."). A lazy unpublishable sequence is refused before it runs.
ScriptResultMaterializer.PublishableScalarTypes / IsPublishable own the set.
Pins: SdkValueBoundaryScriptResultTests (OpcUaServer.Tests, new Core.Scripting ref) checks IsPublishable ==
"FromClr takes it" over 34 values; ProtocolTests checks the wire carries every publishable type scalar + array;
ParityTests gains guid / guid array / guid list / dictionary / KVP[] / lazy KVPs / KVP / decimal / decimal[] /
enum / TimeSpan / char[] / 2-D rows, a Guid unboxing row, and a positive+negative control. Mutations: with the
type check disabled 11 parity tests fail; with Guid not encoded 9 protocol/parity tests fail.
Behaviour change: a value script returning a Dictionary, decimal, enum, TimeSpan, char or tuple now fails
(Bad + script log) in process too, instead of "succeeding" and leaving its node frozen. Remaining divergence:
a jagged array (publish path carries it as Variant[], worker answers "unsupported type") — follow-up.
This commit is contained in:
@@ -220,6 +220,7 @@ message Variant {
|
||||
STRING = 12;
|
||||
DATETIME_UTC = 13;
|
||||
ARRAY = 14;
|
||||
GUID = 15; // lmxopcua#1100 (additive): System.Guid, carried in `g` as its 16 ToByteArray() bytes
|
||||
}
|
||||
TypeTag type = 1; // the exact CLR type: (short)ctx.GetTag(x).Value must keep working
|
||||
oneof v {
|
||||
@@ -231,6 +232,7 @@ message Variant {
|
||||
string s = 7;
|
||||
int64 dt_ticks = 8;
|
||||
ArrayValue array = 9;
|
||||
bytes g = 10; // GUID payload (exactly 16 bytes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,12 @@ public static class VariantCodec
|
||||
variant.Type = Variant.Types.TypeTag.DatetimeUtc;
|
||||
variant.DtTicks = ToUtc(v).Ticks;
|
||||
return true;
|
||||
case Guid v:
|
||||
// lmxopcua#1100: a Guid is an OPC UA built-in (the publish path serves it as one), so it crosses the wire
|
||||
// rather than failing "unsupported type" where the in-process runner gave the client a Guid.
|
||||
variant.Type = Variant.Types.TypeTag.Guid;
|
||||
variant.G = Google.Protobuf.ByteString.CopyFrom(v.ToByteArray());
|
||||
return true;
|
||||
case Array array when array.Rank == 1 && TryElementTag(array.GetType().GetElementType()!, out var elementTag):
|
||||
{
|
||||
var av = new ArrayValue { ElementType = elementTag };
|
||||
@@ -94,6 +100,7 @@ public static class VariantCodec
|
||||
Variant.Types.TypeTag.Double => Expect(variant, Variant.VOneofCase.D).D,
|
||||
Variant.Types.TypeTag.String => Expect(variant, Variant.VOneofCase.S).S,
|
||||
Variant.Types.TypeTag.DatetimeUtc => new DateTime(Expect(variant, Variant.VOneofCase.DtTicks).DtTicks, DateTimeKind.Utc),
|
||||
Variant.Types.TypeTag.Guid => DecodeGuid(Expect(variant, Variant.VOneofCase.G)),
|
||||
Variant.Types.TypeTag.Array => DecodeArray(Expect(variant, Variant.VOneofCase.Array).Array),
|
||||
_ => throw new ScriptWorkerProtocolException($"unknown variant type tag {(int)variant.Type}"),
|
||||
};
|
||||
@@ -117,6 +124,15 @@ public static class VariantCodec
|
||||
return variant;
|
||||
}
|
||||
|
||||
private static Guid DecodeGuid(Variant variant)
|
||||
{
|
||||
// An absent payload is the default value, as for every other tag (see Expect); a present one is exactly 16 bytes.
|
||||
if (variant.VCase == Variant.VOneofCase.None || variant.G.IsEmpty) return Guid.Empty;
|
||||
if (variant.G.Length != 16)
|
||||
throw new ScriptWorkerProtocolException($"variant tagged {variant.Type} carries {variant.G.Length} bytes, not 16");
|
||||
return new Guid(variant.G.Span);
|
||||
}
|
||||
|
||||
private static Array DecodeArray(ArrayValue? array)
|
||||
{
|
||||
if (array is null) throw new ScriptWorkerProtocolException("array variant without an array payload");
|
||||
@@ -149,6 +165,7 @@ public static class VariantCodec
|
||||
_ when elementType == typeof(double) => Variant.Types.TypeTag.Double,
|
||||
_ when elementType == typeof(string) => Variant.Types.TypeTag.String,
|
||||
_ when elementType == typeof(DateTime) => Variant.Types.TypeTag.DatetimeUtc,
|
||||
_ when elementType == typeof(Guid) => Variant.Types.TypeTag.Guid,
|
||||
_ => Variant.Types.TypeTag.Null,
|
||||
};
|
||||
return tag != Variant.Types.TypeTag.Null;
|
||||
@@ -169,6 +186,7 @@ public static class VariantCodec
|
||||
Variant.Types.TypeTag.Double => typeof(double),
|
||||
Variant.Types.TypeTag.String => typeof(string),
|
||||
Variant.Types.TypeTag.DatetimeUtc => typeof(DateTime),
|
||||
Variant.Types.TypeTag.Guid => typeof(Guid),
|
||||
_ => throw new ScriptWorkerProtocolException($"array element type {tag} is not supported"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,7 +32,15 @@ public sealed class ScriptResultException : InvalidOperationException
|
||||
/// (<c>object</c>, an interface, another collection) is refused — the host copies and compares returned arrays
|
||||
/// recursively, so an open-slot array can nest or contain itself (see <c>ForbiddenTypeAnalyzer</c>). Arrays are
|
||||
/// returned as-is (fully built by the run) but are still held to the element cap. Everything else — scalars,
|
||||
/// strings, null — is untouched.
|
||||
/// strings, null — is untouched, provided its type is publishable.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The result's TYPE is checked too (lmxopcua#1100): only what the OPC UA publish path can represent passes —
|
||||
/// see <see cref="PublishableScalarTypes"/> / <see cref="IsPublishable"/>. Anything else (a
|
||||
/// <c>KeyValuePair<K,V>[]</c>, which is what a returned dictionary becomes, a <c>decimal</c>, an enum, a
|
||||
/// <c>TimeSpan</c>, a tuple) fails the run with a <see cref="ScriptResultException"/> in both runners. Before, the
|
||||
/// in-process runner handed it on and the node write threw, leaving the node on its previous value and status
|
||||
/// with only a server-log warning, while the worker answered "unsupported type".
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Concrete collections used to be returned as-is too (lmxopcua#1092): in process the host got the
|
||||
@@ -66,6 +74,7 @@ public static class ScriptResultMaterializer
|
||||
case null or string:
|
||||
return value;
|
||||
case Array array:
|
||||
EnsurePublishable(array.GetType());
|
||||
EnforceCap(array.Length, maxElements);
|
||||
return array;
|
||||
case ICollection collection:
|
||||
@@ -74,10 +83,61 @@ public static class ScriptResultMaterializer
|
||||
case IEnumerable sequence:
|
||||
return MaterializeSequence(sequence, maxElements, IsGenericCollection(sequence.GetType()) ? "collection" : "lazy sequence");
|
||||
default:
|
||||
EnsurePublishable(value.GetType());
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The scalar CLR types a script result may have, alone or as the element type of a one-dimensional array
|
||||
/// (lmxopcua#1100): exactly the types the server's OPC UA value boundary (<c>SdkValueBoundary.FromClr</c>) turns
|
||||
/// into a built-in Variant — Boolean, the eight integer types, Float, Double, String, DateTime and Guid — and,
|
||||
/// with them, the types the out-of-process wire (<c>VariantCodec</c>) carries. Pinned against both by tests.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Type> PublishableScalarTypes { get; } =
|
||||
[
|
||||
typeof(bool), typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
|
||||
typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(DateTime), typeof(Guid),
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Whether a returned value of <paramref name="type"/> can reach an OPC UA client (lmxopcua#1100): a
|
||||
/// <see cref="PublishableScalarTypes"/> scalar, or a one-dimensional array whose element type is publishable
|
||||
/// (a jagged array of them included — the publish path carries it as an array of Variants).
|
||||
/// </summary>
|
||||
/// <param name="type">The runtime type of the returned value.</param>
|
||||
/// <returns><c>true</c> when the publish path can represent the value.</returns>
|
||||
public static bool IsPublishable(Type type)
|
||||
{
|
||||
if (type.IsArray)
|
||||
return type.GetArrayRank() == 1 && !type.IsVariableBoundArray && IsPublishable(type.GetElementType()!);
|
||||
return PublishableScalarTypes.Contains(type);
|
||||
}
|
||||
|
||||
// lmxopcua#1100: a value the OPC UA publish path cannot represent (a KeyValuePair[] — what a returned Dictionary
|
||||
// becomes — a decimal, an enum, a TimeSpan, …) used to "succeed" in process and then fail inside the node write
|
||||
// (SdkValueBoundary.FromClr throws; OpcUaPublishActor logs it), so the node silently kept its previous value under its
|
||||
// previous status, while the worker refused the same script "unsupported type". Refused here, inside the run, it is
|
||||
// the same Bad + script-log failure in both runners.
|
||||
private static void EnsurePublishable(Type type)
|
||||
{
|
||||
if (IsPublishable(type)) return;
|
||||
throw new ScriptResultException(
|
||||
$"The script returned a value of type '{Describe(type)}', which an OPC UA node cannot carry; return null, " +
|
||||
"a bool, an integer type, float, double, string, DateTime or Guid, or a one-dimensional array of one of them " +
|
||||
"(convert first, e.g. Convert.ToDouble(x) for a decimal, or the keys and values of a dictionary as two arrays).");
|
||||
}
|
||||
|
||||
private static string Describe(Type type)
|
||||
{
|
||||
if (type.IsArray) return Describe(type.GetElementType()!) + (type.GetArrayRank() == 1 ? "[]" : $"[{new string(',', type.GetArrayRank() - 1)}]");
|
||||
if (!type.IsGenericType) return type.Name;
|
||||
var name = type.Name;
|
||||
var tick = name.IndexOf('`');
|
||||
if (tick >= 0) name = name[..tick];
|
||||
return $"{name}<{string.Join(", ", type.GetGenericArguments().Select(Describe))}>";
|
||||
}
|
||||
|
||||
private static object MaterializeSequence(IEnumerable sequence, int maxElements, string what)
|
||||
{
|
||||
var elementType = ElementTypeOf(sequence.GetType())
|
||||
@@ -88,6 +148,9 @@ public static class ScriptResultMaterializer
|
||||
throw new ScriptResultException(
|
||||
$"The script returned a {what} of '{elementType.Name}', which can contain another sequence or itself; " +
|
||||
"return a typed array of a concrete type (double[], string[], …).");
|
||||
// Checked before anything is enumerated: a sequence that could only become an unpublishable array is refused
|
||||
// without running it.
|
||||
EnsurePublishable(elementType.MakeArrayType());
|
||||
|
||||
var items = new List<object?>();
|
||||
foreach (var item in sequence)
|
||||
|
||||
Reference in New Issue
Block a user