Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move_to_using_path is not thread-safe #937

Open
infohash opened this issue Feb 4, 2025 · 0 comments
Open

move_to_using_path is not thread-safe #937

infohash opened this issue Feb 4, 2025 · 0 comments
Labels

Comments

@infohash
Copy link

infohash commented Feb 4, 2025

Sometimes you have to move a bunch of files together to another folder. If you use move_to_using_path concurrently using ThreadPoolExecutor, ClientRequestException is raised.

How to reproduce

import concurrent.futures

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.move_operations import MoveOperations

ctx: ClientContext = ClientContext(base_url='https://example.sharepoint.com/sites/tenant').with_user_credentials(
    username='test_user', password='test_password')


def main(source_filename):
    """Moves a file from source folder to destination folder.

    Args:
        source_filename: Name of the file.
    """
    ctx.web.get_file_by_server_relative_path(path=f'Shared Documents/{source_filename}').move_to_using_path(
        destination='Shared Documents/destination_folder', flag=MoveOperations.overwrite).execute_query()


>>> with concurrent.futures.ThreadPoolExecutor() as executor:
...     executor.map(main, ['one.csv', 'two.csv', 'three.csv'])

office365.runtime.client_request_exception.ClientRequestException: ('-2147024809, System.ArgumentException', 'Server relative urls must start with SPWeb.ServerRelativeUrl', "400 Client Error: Bad Request for url: https://example.sharepoint.com/sites/tenant/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2Ftenant%2FShared%20Documents%2Fone.csv')/MoveToUsingPath(DecodedUrl='%2Fone.csv',moveOperations=1)")

It will work fine if there is only one thread.

>>> with concurrent.futures.ThreadPoolExecutor() as executor:
...     executor.map(main, ['one.csv'])

Workaround

Use copy.deepcopy to prevent the statefulness of the object from being overwritten by other threads.

def main(source_filename):
    """Moves a file from source folder to destination folder.

    Args:
        source_filename: Name of the file.
    """
    import copy

    copy.deepcopy(ctx).web.get_file_by_server_relative_path(path=f'Shared Documents/{source_filename}').move_to_using_path(
        destination='Shared Documents/destination_folder', flag=MoveOperations.overwrite).execute_query()
@vgrem vgrem added the bug label Mar 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants