Skip to content

"flutter attach --machine" provides a malformed VM Service URI #118609

Description

@DanTup

The wsUri printed when using flutter attach --machine appears to be malformed:

danny@Dannys-MacBook-Pro flutter_counter % flutter attach --machine -d iPhone --debug-url http://127.0.0.1:49606/h-O0daymeDM=/
[{"event":"daemon.connected","params":{"version":"0.6.1","pid":2769}}]
[{"event":"app.start","params":{"appId":"f0197a9d-8d92-40e6-839a-61d561321b8e","deviceId":"A1F0E6A3-DBEA-4CBE-BACE-3893023E5897","directory":null,"supportsRestart":true,"launchMode":"attach"}}]
[
	{
		"event": "app.debugPort",
		"params": {
			"appId": "f0197a9d-8d92-40e6-839a-61d561321b8e",
			"port": 49606,
	######## wsUri looks wrong here
			"wsUri": "ws://127.0.0.1:49606/h-O0daymeDM=/.,/ws",
			"baseUri": "file:///Users/danny/Library/Developer/CoreSimulator/Devices/A1F0E6A3-DBEA-4CBE-BACE-3893023E5897/data/Containers/Data/Application/B788D5FE-81AD-487E-9B95-8EA6357DFF65/tmp/flutter_counterFG3CJR/flutter_counter/"
		}
	}
]

The wsUri returned is ws://127.0.0.1:49606/h-O0daymeDM=/.,/ws but I would expect it to be ws://127.0.0.1:49606/h-O0daymeDM=/ws.

Activity

  1. added
    toolAffects the "flutter" command-line tool. See also t: labels.
    on Jan 17, 2023
  2. DanTup commented on Jan 17, 2023

    @DanTup
    ContributorAuthor

    If you pass a ws://.../ws for --debug-uri you get the same behaviour. Though if you pass --no-dds you get slightly different results (correct results when passing a http root, but incorrect when passing the ws address):

    // args
    --debug-url  ws://127.0.0.1:51458/Xxj5fo0dHj0=/ws --no-dds
    // wsUri
    ws://127.0.0.1:51458/Xxj5fo0dHj0=/ws/ws
    
    // args
    --debug-url http://127.0.0.1:51458/Xxj5fo0dHj0= --no-dds
    // wsUri
    ws://127.0.0.1:51458/Xxj5fo0dHj0=/ws
    

    @christopherfujino @bkonyi before I start trying to fix anything here, can we confirm what formats of URI should be supported here? I notice that a lot of the integration tests re only using port numbers (and --disable-auth-codes) which means they're not seeing any of this behaviour. I've started changing the tests to leave auth-codes enabled and just handle proper URIs instead of only port numbers for better coverage, but I want to be sure I'm making the right assumptions about what we need to pass for --debug-uri (I'm assuming that the output wsUri should always start ws:/ and end /ws).

  3. christopherfujino commented on Jan 17, 2023

    @christopherfujino
    Contributor

    @christopherfujino @bkonyi before I start trying to fix anything here, can we confirm what formats of URI should be supported here? I notice that a lot of the integration tests re only using port numbers (and --disable-auth-codes) which means they're not seeing any of this behaviour. I've started changing the tests to leave auth-codes enabled and just handle proper URIs instead of only port numbers for better coverage, but I want to be sure I'm making the right assumptions about what we need to pass for --debug-uri (I'm assuming that the output wsUri should always start ws:/ and end /ws).

    Sorry, I can't be of much help here, I don't know how any of this works. Also, is this a recent regression? This seems like a pretty bad breakage.

  4. jason-simmons commented on Jan 18, 2023

    @jason-simmons
    Member

    This is not a recent regression.

    It's happening because flutter_tools is trying to recover a DDS URI by parsing an error message returned in an exception (see https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/base/dds.dart#L74)

    The exception message adds a period after the URI (see https://github.com/dart-lang/sdk/blob/main/sdk/lib/vmservice/vmservice.dart#L274). The message is also formatted in a JSON-like style with comma-delimited fields.

    So the parser captures the ., suffix as part of the URI.

  5. DanTup commented on Jan 18, 2023

    @DanTup
    ContributorAuthor

    Ah, thanks! It looks like we should probably add a field to the exception (or a subclass of this exception) for the URI and then it won't need to be extracted from the message like this. I'll take a look, thanks!

  6. self-assigned this
    on Jan 18, 2023
  7. DanTup commented on Jan 18, 2023

    @DanTup
    ContributorAuthor

    @bkonyi I now realise the code linked above is part of lib/vmservice/vmservice.dart and not inside DDS (and that the error is going over the JSON protocol) so it's not as simple as just changing the exception and adding a field.

    Although, I'm curious why we need to extract the URL here. If there's already a DDS instance can't we use the original VM Service URL? It seems like we're doing that in DAP - we're handling the same exception, but just using the same URL:

    https://github.com/dart-lang/sdk/blob/376886ed1da63f81196c72490830fafd954b87d5/pkg/dds/lib/src/dap/adapters/dart.dart#L618-L620

    I'm not sure if the is a difference in requirements here, or whether maybe DAP isn't doing the correct thing?

  8. bkonyi commented on Jan 18, 2023

    @bkonyi
    Contributor

    If the VM service client is capable of handling redirects, using the VM service URI instead of the DDS URI works fine since the VM service will redirect requests from non-DDS clients to DDS.

    However, if a web client like DevTools tries to make a websocket connection to the VM service directly when there's a DDS instance attached, the connection will fail since the dart:html WebSocket implementation doesn't follow redirects (and there's no way to tell it to follow them). I had originally intended for the DDS URI to be completely transparent to clients, having the VM service redirect requests to DDS, but this restriction made it impossible to rely on redirects which is why we need to output the DDS URI.

  9. DanTup commented on Jan 18, 2023

    @DanTup
    ContributorAuthor

    @bkonyi ah, I see! Is it possible/reasonable to extend the exception in some way to carry this URI over without Flutter needing to parse it? (I couldn't figure out where the error was being deserialised to be used in https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/base/dds.dart#L74 to know whether it could have additional field over just message)

  10. bkonyi commented on Jan 18, 2023

    @bkonyi
    Contributor

    Yeah, we probably could do that. The exception is created here: https://github.com/dart-lang/sdk/blob/376886ed1da63f81196c72490830fafd954b87d5/pkg/dds/lib/src/dds_impl.dart#L214.

    We don't do any processing of the exception in DDS right now, but it's something we could do.

  11. DanTup commented on Jan 18, 2023

    @DanTup
    ContributorAuthor

    Cool, lemme take a look. Thanks!

  12. added a commit that references this issue on Jan 30, 2023
    a351c5a
  13. DanTup commented on Jan 30, 2023

    @DanTup
    ContributorAuthor

    I have a fix at #119506. It makes two changes:

    1. Use a new field on the exception that holds exactly the URI (this requires the existing DDS instance provided it so is not guaranteed since we don't know what version it is if we didn't spawn it)
    2. If we have to fall back to parsing the exception text, we trim trailing fullstops
  14. github-actions commented on Mar 3, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  15. locked as resolved and limited conversation to collaborators on Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

toolAffects the "flutter" command-line tool. See also t: labels.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions