Skip to content

Value error for string shared memory in multiprocessing #76472

Closed

Description

@magu
mannequin
BPO 32291
Nosy @ezio-melotti, @hongweipeng, @tirkarthi, @iritkatriel

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 = None
closed_at = <Date 2021-11-05.19:46:29.374>
created_at = <Date 2017-12-12.17:00:36.338>
labels = ['invalid', 'library', 'expert-unicode', 'type-crash']
title = 'Value error for string shared memory in multiprocessing'
updated_at = <Date 2021-11-05.19:46:29.373>
user = 'https://bugs.python.org/magu'

bugs.python.org fields:

activity = <Date 2021-11-05.19:46:29.373>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2021-11-05.19:46:29.374>
closer = 'iritkatriel'
components = ['Library (Lib)', 'Unicode']
creation = <Date 2017-12-12.17:00:36.338>
creator = 'magu'
dependencies = []
files = []
hgrepos = []
issue_num = 32291
keywords = []
message_count = 4.0
messages = ['308144', '326673', '326698', '404145']
nosy_count = 5.0
nosy_names = ['ezio.melotti', 'magu', 'hongweipeng', 'xtreak', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue32291'
versions = ['Python 3.6']

Activity

  1. magu commented on Dec 12, 2017

    magumannequin
    MannequinAuthor

    It seems like sharing a string over processes is not possible.

    	#!/usr/bin/env python3
    	
    	import multiprocessing
    	import threading
    	import ctypes
    	
    	
    	def fun(share_c, share_s, set_c, set_s, name):
    		print(f'{name}: {share_c.value}; {share_s.value}')
    		share_c.value = set_c
    		share_s.value = set_s
    		print(f'{name}: {share_c.value}; {share_s.value}')
    	
    	
    	if __name__ == '__main__':
    		share_c = multiprocessing.Value(ctypes.c_wchar, 'a')
    		share_s = multiprocessing.Value(ctypes.c_wchar_p, 'aa')
    	
    		print(f'pre_thread: {share_c.value}; {share_s.value}')
    		thread = threading.Thread(target=fun, args=(share_c, share_s, 'b', 'bb', 'thread'))
    		thread.start()
    		thread.join()
    	
    		print(f'post_thread: {share_c.value}; {share_s.value}')
    		process = multiprocessing.Process(target=fun, args=(share_c, share_s, 'c', 'cc', 'process'))
    		process.start()
    		process.join()
    	
    		print(f'post_process: {share_c.value}', end='; ')
    		print(share_s.value)  # <--- Blows here

    produces:

    	pre_thread: a; aa
    	thread: a; aa
    	thread: b; bb
    	post_thread: b; bb
    	process: b; bb
    	process: c; cc
    	post_process: c; Traceback (most recent call last):
    	  File "test2.py", line 30, in <module>
    	    print(share_s.value)  # <--- Blows here
    	  File "<string>", line 5, in getvalue
    	ValueError: character U+ff92f210 is not in range [U+0000; U+10ffff]

    Where the character value in the error message is different every time. To me this seems like a bug as it is working properly with threads as well as single characters. (Maybe relevant question also here: https://stackoverflow.com/questions/47763878/how-to-share-string-between-processes?noredirect=1#comment82492062_47763878)

    For the case it matters:
    Python 3.6.1 (Anaconda 4.4.0) on RHEL 6

  2. added
    stdlib
    type-crash
    on Dec 12, 2017
  3. hongweipeng commented on Sep 29, 2018

    hongweipengmannequin
    Mannequin

    This problem seems to be support for str.

    import multiprocessing
    import ctypes
    
    def child_process_fun(share):
        share.value = 'bb'
    
    if __name__ == '__main__':
        share = multiprocessing.Value(ctypes.c_wchar_p, 'aa')
        process = multiprocessing.Process(target=child_process_fun, args=(share, ))
        process.start()
        process.join()
        print(share.value)

    It also raises ValueError.When use c_double or c_int, it works well.

    Test in python3.7 and 3.8

  4. hongweipeng commented on Sep 30, 2018

    hongweipengmannequin
    Mannequin

    I think I know the reason. c_wchar_p corresponds to the string pointer wchar_t *.It's not a good idea to pass pointers between processes. As quoted from multiprocessing docs:

    Note Although it is possible to store a pointer in shared memory remember that this will refer to a location in the address space of a specific process. However, the pointer is quite likely to be invalid in the context of a second process and trying to dereference the pointer from the second process may cause a crash.

    https://docs.python.org/3.6/library/multiprocessing.html?#module-multiprocessing.sharedctypes

  5. iritkatriel commented on Oct 17, 2021

    @iritkatriel
    Member

    Is there anything left to do here?

    It seems hongweipeng's explanation and the link to the documentation pretty much cover it.

  6. 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

No one assigned

    Labels

    stdlibtopic-unicodetype-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions