Make the FTP extension coroutine-enabled #20301
                
     Closed
            
            
          
      
        
          +19
        
        
          −3
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Hello everyone,
In the issue swoole/swoole-src#5890, a developer inquired about the possibility of enabling coroutine support for the FTP extension in a Swoole environment. After a thorough analysis of the PHP FTP extension source code, we identified that the current implementation of the
my_pollfunction uses a hardcoded traditionalpollmechanism to handle socket read/write events.This synchronous I/O model lacks the flexibility of an underlying event-driven architecture, causing FTP operations to block the entire process and preventing effective collaboration with Swoole's coroutine scheduler. Specifically, when an FTP read/write operation is initiated,
my_pollenters a blocking wait state, during which coroutine switching cannot occur, thereby breaking the concurrency of Swoole coroutines.To resolve this issue, we have refactored the
my_pollfunction inftp.cwith the following key modifications:Replace
php_pollfd_for_mswith a function pointer namedphp_ftp_pollfd_for_msto achieve a pluggable architecture for the FTP socket polling mechanism.During the extension initialization phase, this pointer defaults to pointing to the original php_pollfd_for_ms implementation, ensuring that existing synchronous logic remains unaffected.
In the Swoole extension, this function pointer can be replaced with a coroutine-supported polling function (such as a non-blocking version based on an event loop), thereby enabling non-blocking FTP socket operations in the main thread.
Through these improvements, the FTP extension can now deeply integrate with Swoole's coroutine runtime, enabling truly non-blocking, coroutine-friendly FTP client operations.
This modification does not affect the original FTP logic and introduces no syntactic changes.
With this change, I can easily assign a value to
php_ftp_pollfd_for_msin my own extension, enabling coroutine support for FTP without any disruptive intrusions into the FTP code.