How does MinVersion work on union type?

31 views
Skip to first unread message

Chung-sheng Wu

unread,
Oct 12, 2021, 1:08:50 AM10/12/21
to chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Hi all,

On a stable union type, we can use MinVersion to add new fields. But what happen if a client receive a union from a newer server which have a newer value?
For enum type, the value will be mapped to the field with [Default] tag. However, this is not the case for union. Using "switch(MyUnion::Tag)" may cause the client crash because of unexpected value in MyUnion::Tag.
If the above understanding is correct, it seems like using MinVersion on union is not safe. What is the best practice of doing versioning on union types?

Thanks
Chungsheng

Ken Rockot

unread,
Oct 12, 2021, 11:57:02 AM10/12/21
to Chung-sheng Wu, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Unions tagged with a field unknown to the receiver are decoded as null values. For this to work well, the union-typed field must be marked as optional. For example:

  union Thing {
    int32 number;
    string name;
    [MinVersion=1] string? animal;
  };

  struct SomeOtherStruct {
    // This field will be null if someone sends an animal to a recipient
    // using Thing v0.
    Thing? thing;
  };
 
If, on the other hand, the union-typed field is not marked optional, then the receiver will behave like any other scenario where a non-optional field receives a null value: it will fail validation, reject the message, and disconnect the interface.


Thanks
Chungsheng

--
You received this message because you are subscribed to the Google Groups "chromium-mojo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-moj...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/CA%2Bzq0vDRhnt%3DFo58s6jQDLnY%3DO8bsEzqGVELj-RRnOfM5_ojyA%40mail.gmail.com.

Chung-sheng Wu

unread,
Oct 13, 2021, 7:46:00 AM10/13/21
to Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Thanks for the explanation!

Ken Rockot <roc...@google.com> 於 2021年10月12日 週二 下午11:57寫道:

Chung-sheng Wu

unread,
Oct 14, 2021, 2:04:02 AM10/14/21
to Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Hi Ken,

More questions.

1. What is the difference between |string| and |string?| in a union? Does it make string become optional<string>?

2. What if the users using |switch(MyUnion->which()) {...}| ? Does the switch get an unexpected enum value and crash?(assume that no |default:| in that switch statement.) What value will |which()| return from a null union?

Thanks
Chungsheng

Chung-sheng Wu <chung...@google.com> 於 2021年10月13日 週三 下午7:45寫道:

Daniel Cheng

unread,
Oct 14, 2021, 2:17:24 AM10/14/21
to Chung-sheng Wu, Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
1. With `string`, the union field type will be std::string. With `string?`, the union field type will be absl::optional<std::string>.

2. Using a versioned union requires that all uses of the union be marked nullable. Then, when Mojo sees an unknown union field, it will be deserialized as a null union instead—so there will be no which() to consult.

Daniel

Chung-sheng Wu

unread,
Oct 14, 2021, 11:32:13 PM10/14/21
to Daniel Cheng, Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Hi,

I did some experiment and faced some issues.
I try to send a union with the following type:
union U {
  int32 a;
  int64 b;
  [MinVersion=1] BarB? c;
};
And receive the union with the following type:
union U {
  int32 a;
  int64 b;
}; 
When the server sent the union with type BarB, I got "Invalid message: VALIDATION_ERROR_UNKNOWN_UNION_TAG (unknown tag in U)".
I also tried to wrap the union in the following struct:
struct UO {
  U? u;
};
But got the same result.
Does I misunderstand something? 

The experiment is in this cl.

Thanks
Chungsheng


Daniel Cheng <dch...@chromium.org> 於 2021年10月14日 週四 下午2:17寫道:

Chung-sheng Wu

unread,
Oct 15, 2021, 3:03:40 AM10/15/21
to Daniel Cheng, Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Also, it seems like union doesn't support ordinal value?

Chung-sheng Wu <chung...@google.com> 於 2021年10月15日 週五 上午11:31寫道:

Chung-sheng Wu

unread,
Oct 17, 2021, 7:35:36 PM10/17/21
to Daniel Cheng, chromi...@chromium.org, Ken Rockot, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng

Friendly  ping on this.

Ken Rockot

unread,
Oct 18, 2021, 8:24:45 AM10/18/21
to Chung-sheng Wu, Daniel Cheng, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
After taking a look at the union code generation, it seems like we never properly implemented union versioning after all, and we have only gotten away with this because nobody's tried using it until now. I don't see any logic in the generated code which would allow for extensibility of the tag enum.

Chung-sheng Wu

unread,
Oct 19, 2021, 12:46:29 AM10/19/21
to Ken Rockot, Meng-Huan Yu, Daniel Cheng, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng

Hi, 

Is there any plan or schedule for the union? It is a critical blocker for us if the union cannot be versioned.

Thanks
Chungsheng

Ken Rockot <roc...@google.com> 於 2021年10月18日 週一 下午8:24寫道:

Meng-Huan Yu

unread,
Oct 19, 2021, 12:55:39 AM10/19/21
to Chung-sheng Wu, Ken Rockot, Daniel Cheng, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Add more context:
We heavily use union in our interface. The interface is actively used between Chrome and Chrome OS and many (our team and our client teams) tests get broken frequently because they are out of sync between Chrome/ChromeOS. After some study, we think versioning is the best approach and we want to be versioning ASAP.

Please advise us how to deal with union versioning if this is not in your priority.

Daniel Cheng

unread,
Oct 19, 2021, 1:04:39 AM10/19/21
to Meng-Huan Yu, Chung-sheng Wu, Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
We should almost certainly fix it. I've already filed a bug to track this: https://crbug.com/1261313.

As it's pretty late for most //mojo OWNERS though, we will discuss more about how to proceed tomorrow.

Daniel

Chung-sheng Wu

unread,
Oct 19, 2021, 1:23:12 AM10/19/21
to Daniel Cheng, Meng-Huan Yu, Ken Rockot, chromi...@chromium.org, Chrome OS Telemetry/Diagnostics/Manageability Taipei Eng
Sounds good! 

Thanks
Chungsheng

Daniel Cheng <dch...@chromium.org> 於 2021年10月19日 週二 下午1:04寫道:
Reply all
Reply to author
Forward
0 new messages