Skip to content

Unable to copy ssl.SSLContext #77204

Closed
Closed

Description

@vitaly-krugl
BPO 33023
Nosy @pitrou, @tiran, @ned-deily, @serhiy-storchaka, @miss-islington, @vitaly-krugl
PRs
  • bpo-33023: SSL pickle error message #6099
  • bpo-33023: Fix NotImplemented to NotImplementedError. #10934
  • [3.7] bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934) #11000
  • [3.6] bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934). #11001
  • [2.7] bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934). (GH-11001) #11008
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/tiran'
    closed_at = <Date 2021-04-17.18:24:35.232>
    created_at = <Date 2018-03-08.06:09:21.578>
    labels = ['expert-SSL', 'type-bug', '3.8']
    title = 'Unable to copy ssl.SSLContext'
    updated_at = <Date 2021-04-17.18:24:35.232>
    user = 'https://github.com/vitaly-krugl'

    bugs.python.org fields:

    activity = <Date 2021-04-17.18:24:35.232>
    actor = 'christian.heimes'
    assignee = 'christian.heimes'
    closed = True
    closed_date = <Date 2021-04-17.18:24:35.232>
    closer = 'christian.heimes'
    components = ['SSL']
    creation = <Date 2018-03-08.06:09:21.578>
    creator = 'vitaly.krug'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33023
    keywords = ['patch']
    message_count = 24.0
    messages = ['313422', '313437', '313458', '313459', '313621', '313703', '313704', '313731', '314322', '314347', '314367', '314375', '314382', '314417', '314419', '318894', '318899', '318903', '328980', '331151', '331247', '331248', '331249', '331270']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'christian.heimes', 'ned.deily', 'serhiy.storchaka', 'miss-islington', 'vitaly.krug']
    pr_nums = ['6099', '10934', '11000', '11001', '11008']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33023'
    versions = ['Python 3.8']

    Activity

    1. vitaly-krugl commented on Mar 8, 2018

      vitaly-kruglmannequin
      MannequinAuthor
      import copy
      import ssl
      
      copy.copy(ssl.create_default_context())
      

      results in

      TypeError: can't pickle SSLContext objects

      This prevents me from being able to copy.deepcopy() an object that references ssl.SSLContext.

      The apparent root cause is apparently that ssl.SSLContext passes an extra arg to its __new__ method, but doesn't implement the method __getnewargs__ that would let copy extract the extra arg.

    2. added
      type-crash
      on Mar 8, 2018
    3. tiran commented on Mar 8, 2018

      @tiran
      Member

      This is rather a feature than a bug. It is not possible to make a copy of a SSLContext object because OpenSSL doesn't support the operation. A context contains elements that can't be cloned easily, e.g. session resumption tickets.

    4. vitaly-krugl commented on Mar 8, 2018

      vitaly-kruglmannequin
      MannequinAuthor

      Hi Christian, thank you for following up. Here is my use case, and perhaps you can suggest something else that will work:

      I am refactoring the transport layer in the Pika AMQP client library. The user provides an ssl.SSLContext instance for connecting to an AMQP broker (e.g., RabbitMQ). Pika will resolve the hostname via getaddrinfo and make attempts to establish TCP and AMQP connection to the candidate IP addresses until one succeeds in establishing an AMQP connection over SSL. Each connection attempt will require a fresh unadulterated clone of the ssl.SSLContext instance provided by user to avoid any side-effects from prior connection attempts.

      How can I obtain this pristine clone cleanly for each new connection attempt?

    5. vitaly-krugl commented on Mar 8, 2018

      vitaly-kruglmannequin
      MannequinAuthor

      Also, updating ssl.SSLContext documentation about intentional inability to copy generically and suggestion how to go about it if you need to obtain a clone or similar would be terrific and save developers time so they won't run into this gotcha when designing and implementing solutions. Many thanks!

    6. pitrou commented on Mar 11, 2018

      @pitrou
      Member

      Each connection attempt will require a fresh unadulterated clone of the ssl.SSLContext instance provided by user to avoid any side-effects from prior connection attempts.

      What would those side-effects be?

    7. added
      type-bug
      and removed
      type-crash
      on Mar 11, 2018
    8. vitaly-krugl commented on Mar 12, 2018

      vitaly-kruglmannequin
      MannequinAuthor

      What would those side-effects be?

      Christian Heimes suggested that

      A context contains elements that can't be cloned easily, e.g. session resumption tickets.

      My concern then would be potential side-effects from such session resumption tickets and anything else that one connection attempt might save/change within an SSL Context that might have an undesirable side-effect on the follow-on connection attempts.

    9. pitrou commented on Mar 12, 2018

      @pitrou
      Member

      You won't have any session resumption tickets until a connection succeeds. And even then, I don't think it would be a problem. By design, SSL contexts are meant to be re-used accross multiple connections.

    10. tiran commented on Mar 13, 2018

      @tiran
      Member

      Antoine is correct. Session resumption is fully transparent for the application layer. The context object is designed to be reused by multiple connections to same or different servers.

    11. serhiy-storchaka commented on Mar 23, 2018

      @serhiy-storchaka
      Member

      PR 6099 changes error message "can't pickle SSLContext objects" to "cannot serialize SSLContext object", right? Wouldn't be better to change the standard error message instead?

    12. vitaly-krugl commented on Mar 24, 2018

      vitaly-kruglmannequin
      MannequinAuthor

      It would be very helpful to make a statement in SSLContext's documentation to the effect that it's not copyable. This is frankly the first time I run into a non-copyable object.I spend quite a bit of time researching this after implementing a copying strategy that failed. It would have saved me (and others...) so much time is there was a warning in SSLContext documentation about not being able to serialize/copy/deepcopy by design!

      Also, making that exception message more generic (ha, I wasn't pickling anything?!) as Serhiy Storchaka suggested would be a welcome addition, but not replacement for documentation.

    13. 2 remaining items

    14. serhiy-storchaka commented on Mar 24, 2018

      @serhiy-storchaka
      Member

      Christian, I thought the reason of adding __getstate__ methods which raise an exception is that the existing error message doesn't satisfy your. What is the other reason?

    15. tiran commented on Mar 25, 2018

      @tiran
      Member

      That's the only reason for PR 6099. The change makes it more obvious that SSL objects can't be serialized or copied.

    16. serhiy-storchaka commented on Mar 25, 2018

      @serhiy-storchaka
      Member
    17. ned-deily commented on Jun 7, 2018

      @ned-deily
      Member

      We have a languishing PR here. What should be done with it? If I understand correctly, Serhiy is proposing a more general cleanup in bpo-33138 PR 6239. If that is merged, can PR 6099 here be simplified? What branches should it apply to? (retargeting for 3.8 pending comments)

    18. serhiy-storchaka commented on Jun 7, 2018

      @serhiy-storchaka
      Member

      The change to the dup() method looks as a bug fix to me. It needs to be backported to all maintained versions. New tests can be backported to all versions except perhaps 2.7 (not tested). Changes to __getstate__() will be not needed if merge bpo-33138 PR 6239 (they just change the error message). But it may be needed to apply them to 2.7 only.

    19. serhiy-storchaka commented on Jun 7, 2018

      @serhiy-storchaka
      Member

      If changes to __getstate__() be merged in 2.7, I suggest to unify them with bpo-33138.

    20. serhiy-storchaka commented on Oct 31, 2018

      @serhiy-storchaka
      Member

      After discussing on Python-Dev all error messages were changed to "cannot pickle 'XXX' object".

      https://mail.python.org/pipermail/python-dev/2018-October/155599.html

      I suggest to not add __getstate__() methods with different error messages. But the dup() method should be fixed.

    21. serhiy-storchaka commented on Dec 5, 2018

      @serhiy-storchaka
      Member

      PR 10934 fixes just a typo in the name of NotImplementedError. It fixes also a similar typo in IDLE. It adds new tests and fixes existing tests for NotImplementedError in SSLSocket.

    22. serhiy-storchaka commented on Dec 6, 2018

      @serhiy-storchaka
      Member

      New changeset 42b1d61 by Serhiy Storchaka in branch 'master':
      bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934)
      42b1d61

    23. miss-islington commented on Dec 6, 2018

      @miss-islington
      Contributor

      New changeset 6485aa6 by Miss Islington (bot) in branch '3.7':
      bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934)
      6485aa6

    24. serhiy-storchaka commented on Dec 6, 2018

      @serhiy-storchaka
      Member

      New changeset 7a2cf1e by Serhiy Storchaka in branch '3.6':
      bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934). (GH-11001)
      7a2cf1e

    25. serhiy-storchaka commented on Dec 7, 2018

      @serhiy-storchaka
      Member

      New changeset 324e179 by Serhiy Storchaka in branch '2.7':
      [2.7] bpo-33023: Fix NotImplemented to NotImplementedError. (GH-10934). (GH-11001) (GH-11008)
      324e179

    26. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.8 (EOL)topic-SSLtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions