Skip to content

"salt.utils.fopen(path, 'rb')" may cause error #16751

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

Closed
derek0377 opened this issue Oct 20, 2014 · 5 comments
Closed

"salt.utils.fopen(path, 'rb')" may cause error #16751

derek0377 opened this issue Oct 20, 2014 · 5 comments
Assignees
Labels
Bug broken, incorrect, or confusing behavior Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged stale
Milestone

Comments

@derek0377
Copy link

here is my scene:
in salt\modules\saltutil.py line 421 ,

     function _read_proc_file
     line:  with salt.utils.fopen(path, 'rb') as fp_:
                     buf = fp_.read()
                     fp_.close()
                     if buf:
                           data = serial.loads(buf)   #mspack load here
                     else:

----------------------------------------------------code finish-------------------------------------------------
if path's file'content is:

劊fun玬ine.updateid?0141017165429682000id?
寓idVR6175

if use 'rb' mode to read the file,buf will be:

劊fun玬ine.updateid?0141017165429682000id?

寓idVR6175

The content readed contain a '\r' more than the orginal content(on windows 7 plat-form, '\n' will change to '\r\n' through the open(file,'rb').read() ), and this cause a error when the file is mspack's format,because an additional '\r' causes mspack loading failed.

But I try to modify the 'rb' mode to 'r' mode, the problem miss. ('\n' still to be '\n' through the open(file,'r').read() )
So is it a real problem?

@derek0377 derek0377 changed the title there are many "salt.utils.fopen(path, 'rb')" in salt's code,and they may cause error "salt.utils.fopen(path, 'rb')" may cause error Oct 20, 2014
@cro
Copy link
Contributor

cro commented Oct 20, 2014

Can you demonstrate where this causes a problem? We've been over this code extensively. Not opening files in Binary mode on Windows causes a lot of trouble for most Python software that needs to run on both Windows and Unix-like machines.

@cro cro modified the milestones: Approved, Blocked Oct 20, 2014
@cro cro self-assigned this Oct 20, 2014
@cachedout cachedout added Bug broken, incorrect, or confusing behavior Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged labels Oct 20, 2014
@derek0377
Copy link
Author

when I call "saltutil.running" function: a error throw:

salt 'test' saltutil.running
test:
The minion function caused an exception: Traceback (most recent call last):
File "salt/minion.py", line 797, in _thread_return
File "salt/modules/saltutil.py", line 456, in running
File "salt/modules/saltutil.py", line 433, in _read_proc_file
KeyError: 'pid'

Part of code of function _read_proc_file:

---------------code begin-------------------------------------

def _read_proc_file(path):
        '''
        Return a dict of JID metadata, or None
        '''
        with salt.utils.fopen(path, 'rb') as fp_:
            buf = fp_.read()
            fp_.close()
            if buf:
                data = serial.loads(buf)
            else:

--------------code finish------------------------------------------

look into the salt/modules/saltutil.py line 425, this problem is cause by 'buf = fp_.read()', the buffer contains an additional '\r', leads exception in line 425(data = serial.loads(buf)), If use 'r' mode, serial.loads success because no more '\r' in buf.

image

The original file just contains the '\n'

@olliewalsh
Copy link
Contributor

The issue is that the file was writing in text mode, not that it's being read in binary mode.

@derek0377
Copy link
Author

@olliewalsh, It is written in w+b mode,see below

salt\minion.py

def _thread_return(cls, minion_instance, opts, data):
    '''
    This method should be used as a threading target, start the actual
    minion side execution.
    '''
   。。。。。
    with salt.utils.fopen(fn_, 'w+b') as fp_:
        fp_.write(minion_instance.serial.dumps(sdata))

@stale
Copy link

stale bot commented Dec 4, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

@stale stale bot added the stale label Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged stale
Projects
None yet
Development

No branches or pull requests

4 participants