Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

perf: Function library execution

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented May 30, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented May 30, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 merged commit 7ce66a7 into main May 30, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_function_lib branch May 30, 2025 10:56
os.system(f"chown {self.user}:root {exec_python_file}")
kwargs = {'cwd': BASE_DIR}
subprocess_result = subprocess.run(
['su', '-s', python_directory, '-c', "exec(open('" + exec_python_file + "').read())", self.user],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several potential issues and optimizations in the provided code:

  1. Permissions Handling:

    • The code uses os.chown to set ownership of files, which can be dangerous. If not used carefully, it could expose sensitive information or lead to permissions issues.
    • It's better to handle file access and execution using appropriate filesystem permissions rather than relying on permission changes.
  2. File Write Permissions:

    • There is no explicit check for write permissions when writing to the sandbox directory paths. Ensure that these directories have write permissions.
  3. Error Logging and Cleanup:

    • Improper error handling: The code doesn't log errors properly. If an exception occurs during file operations or subprocess execution, it should be logged for debugging purposes.
    • Potential issue with temporary files: When executing Python scripts in a sandboxed environment, ensure that any temporary files created are deleted after they're no longer needed.
  4. Use of DiskCache for Caching:

    • While DiskCache is being used for caching results, its use needs to be consistent with the rest of the codebase. There's confusion between using DiskCache directly versus pickle.
  5. Code Formatting:

    • The formatting of the _exec_code string lacks consistency with other parts of the function.
    • Consider cleaning up unnecessary whitespace before returning subprocess_result.
  6. Resource Management:

    • Always ensure that resources like file descriptors are closed after use, especially when dealing with I/O operations and subprocesses.

Optimizations and Recommendations

  1. Permission Best Practices:

    • Use chroot/jail environments instead of trying to change user/group IDs inside a process. This is more secure and reliable.
    • Restrict file system access to only necessary components within the sandbox path.
  2. Consistent Error Logging:

    • Implement logging at all stages where errors occur. A basic logging setup might look like this:
      import logging
      logger = logging.getLogger(__name__)
      logger.setLevel(logging.ERROR)
      
      handler = logging.FileHandler("app.log")
      formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
      handler.setFormatter(formatter)
      logger.addHandler(handler)
  3. Temporary File Cleanup:

    • After executing the script, delete any temporary files generated by the script and any related output files.
  4. Consistent Code Style:

    • Refactor _exec_code to remove unnecessary whitespace and align comments.
  5. Use Context Managers:

    • Prefer context managers (e.g., with open(...) as ...) over manual file closing to avoid resource leaks.

Here's an improved version of the relevant part of the code:

def exec_code(self, code_str, keywords):
    try:
        _id = str(uuid.uuid1())
        
        # Create required directories
        old_mask = os.umask(0o077)
        try:
            os.makedirs(self.sandbox_path, 0o700, exist_ok=True)
            os.makedirs(os.path.join(self.sandbox_path, 'execute'), 0o700, exist_ok=True)
            os.makedirs(os.path.join(self.sandbox_path, 'result'), 0o700, exist_ok=True)
        finally:
            os.umask(old_mask)
            
        # Execute the code
        _exec_code = f"""
try:
    import os
...
"""

        if self.sandbox:
            self._exec_sandbox(_exec_code, _id)
        else:
            subprocess_result = self._exec(_exec_code)

        if subprocess_result.returncode != 0:
            raise Exception(subprocess_result.stderr)
            
        # Read and close the cached response correctly
        with open(os.path.join(self.sandbox_path, 'result', f"{_id}.result"), 'rb') as file:
            result = pickle.loads(file.read())

        os.remove(os.path.join(self.sandbox_path, 'result', f"{_id}.result"))
        return result.get('data')

    except Exception as e:
        self.logger.error(f'An error occurred while executing code: {e}')
        return {"code": 500, "msg": str(e), "data": None}

def _exec_sandbox(self, _code, _id):
    exec_python_file = os.path.join(self.sandbox_path, 'execute', f"{_id}.py")
    
    with open(exec_python_file, 'w') as file:
        file.write(_code)

    # Using su for security reasons
    subprocess.run(
        ['su', '-s', python_directory, '-c', f"exec(open('{exec_python_file}').read())"],
        shell=False,
        cwd=BASE_DIR,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    ).check_output()

This revised code includes error logging, ensures consistent cleanup of temporary files, and improves overall structure and readability. Make sure to integrate this logging mechanism into your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants