Repository navigation
Commit 77d4e4b
committed
Keep the acknowledgement first when the transport closes during a listen registration
## Motivation and Context
A `subscriptions/listen` stream is registered before its acknowledgement is written,
and the acknowledgement was written outside any lock. Closing the transport snapshots the registry,
then writes each stream's `SubscriptionsListenResult` under the stream's write mutex and closes it.
When the close landed between the registration and the acknowledgement write, the client received
the result and the stream closed; the acknowledgement write then failed against the closed stream.
SEP-2575 requires the acknowledgement to be the first message on the stream, so such a stream ended
in a shape the specification does not allow.
The acknowledgement is now written under the same write mutex, and it is skipped when the transport has
already marked the entry closed. Closing the transport writes the result only to streams that were acknowledged;
a stream that never was is closed without one, an abrupt close the specification allows and the client answers
by sending `subscriptions/listen` again. Since both sides serialize on the one mutex, whenever both messages
are written the acknowledgement precedes the result, and no result reaches a stream that received no acknowledgement.
The flag that marks a stream acknowledged is set under the registry lock as well as the write mutex,
since the registry lock is the one a notification's delivery snapshot reads it under; nothing holds
the registry lock while waiting for a write mutex, so the two cannot deadlock.
## How Has This Been Tested?
New tests in `test/mcp/server/transports/streamable_http_transport_test.rb` close the transport while
an acknowledgement write is in progress and check that the stream carries the acknowledgement and then
the result, and close it with a registered but unacknowledged stream and check that no result is written.
Against the previous library the first stream carries only the result.
## Breaking Changes
None. A listen stream that the transport closes before its acknowledgement was written now ends without
a result instead of receiving one first.1 parent b56d007 commit 77d4e4b
2 files changed
Lines changed: 112 additions & 22 deletions
File tree
- lib/mcp/server/transports
- test/mcp/server/transports
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | | - | |
| 932 | + | |
933 | 933 | | |
934 | 934 | | |
935 | 935 | | |
936 | 936 | | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
937 | 942 | | |
938 | 943 | | |
939 | 944 | | |
940 | 945 | | |
941 | 946 | | |
942 | 947 | | |
943 | | - | |
| 948 | + | |
944 | 949 | | |
945 | | - | |
946 | | - | |
947 | | - | |
948 | | - | |
| 950 | + | |
| 951 | + | |
949 | 952 | | |
950 | 953 | | |
| 954 | + | |
951 | 955 | | |
952 | 956 | | |
953 | 957 | | |
954 | | - | |
| 958 | + | |
955 | 959 | | |
956 | 960 | | |
957 | 961 | | |
| |||
964 | 968 | | |
965 | 969 | | |
966 | 970 | | |
967 | | - | |
968 | | - | |
969 | | - | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
970 | 992 | | |
971 | 993 | | |
972 | 994 | | |
| |||
975 | 997 | | |
976 | 998 | | |
977 | 999 | | |
978 | | - | |
979 | | - | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | | - | |
985 | | - | |
986 | | - | |
987 | 1000 | | |
988 | 1001 | | |
989 | 1002 | | |
| |||
1149 | 1162 | | |
1150 | 1163 | | |
1151 | 1164 | | |
1152 | | - | |
1153 | | - | |
| 1165 | + | |
| 1166 | + | |
1154 | 1167 | | |
1155 | 1168 | | |
1156 | 1169 | | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
1157 | 1174 | | |
1158 | 1175 | | |
1159 | 1176 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6125 | 6125 | | |
6126 | 6126 | | |
6127 | 6127 | | |
6128 | | - | |
| 6128 | + | |
6129 | 6129 | | |
6130 | 6130 | | |
6131 | 6131 | | |
| |||
6149 | 6149 | | |
6150 | 6150 | | |
6151 | 6151 | | |
| 6152 | + | |
| 6153 | + | |
| 6154 | + | |
| 6155 | + | |
| 6156 | + | |
| 6157 | + | |
| 6158 | + | |
| 6159 | + | |
| 6160 | + | |
| 6161 | + | |
| 6162 | + | |
| 6163 | + | |
| 6164 | + | |
| 6165 | + | |
| 6166 | + | |
| 6167 | + | |
| 6168 | + | |
| 6169 | + | |
| 6170 | + | |
| 6171 | + | |
| 6172 | + | |
| 6173 | + | |
| 6174 | + | |
| 6175 | + | |
| 6176 | + | |
| 6177 | + | |
| 6178 | + | |
| 6179 | + | |
| 6180 | + | |
| 6181 | + | |
| 6182 | + | |
| 6183 | + | |
| 6184 | + | |
| 6185 | + | |
| 6186 | + | |
| 6187 | + | |
| 6188 | + | |
| 6189 | + | |
| 6190 | + | |
| 6191 | + | |
| 6192 | + | |
| 6193 | + | |
| 6194 | + | |
| 6195 | + | |
| 6196 | + | |
| 6197 | + | |
| 6198 | + | |
| 6199 | + | |
| 6200 | + | |
| 6201 | + | |
| 6202 | + | |
| 6203 | + | |
| 6204 | + | |
| 6205 | + | |
| 6206 | + | |
| 6207 | + | |
| 6208 | + | |
| 6209 | + | |
| 6210 | + | |
| 6211 | + | |
| 6212 | + | |
| 6213 | + | |
| 6214 | + | |
| 6215 | + | |
| 6216 | + | |
| 6217 | + | |
| 6218 | + | |
| 6219 | + | |
| 6220 | + | |
| 6221 | + | |
| 6222 | + | |
| 6223 | + | |
| 6224 | + | |
6152 | 6225 | | |
6153 | 6226 | | |
6154 | 6227 | | |
| |||
0 commit comments