-
Notifications
You must be signed in to change notification settings - Fork 262
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
Invalid argument error with libnetcdf 4.5.0 but not 4.4.1 #657
Comments
I can reproduce with conda-forge libnetcdf 4.5.0 on my osx machine as well. |
I believe we have fixed this with a pull to revert to 4.4.1.1 behavior based on a bug report issued by @czender. Let me confirm and follow up with specifics. |
It is not corrected, looking at it now. |
Ok, the issue relates to how padding is being calculated in v1hpg.c:v1h_get_NC_string(). This code was contributed to us by @wkliao so taking a bit to read it and understand it. |
Many thanks for investigating this @dopplershift and @WardF. We have no issues sticking with version 4.4.1.1 for now, but we just wanted to bring this to your attention (also in case it impacts anyone else). |
Thanks for the report! |
FYI. @WardF is referring to PR #332, which checks the null-byte padding in file header. After reading this PR again, I found that returning error code NC_ENOTNC is better than NC_EINVAL in this case, as the error message corresponds to NC_ENOTNC is "NetCDF: Unknown file format", which includes the case when file fails to conform with the classic CDF file format specification. In this case, it is the null-byte padding. FYI. PnetCDF returns NC_ENOTNC. |
But...the file is a valid, so I'm not clear on why it's an error at all. |
@wkliao I can make that change if need be but I need to determine why the file is invalid and, if it is, is it invalid in a way that contradicts the spec but became valid through common usage and the spec not being enforced historically. I will follow up when I get in to the office and can inspect the file more closely. |
I guess the nc file in question violates only the null-byte padding requirement by the CDF format specification. The reason it can be read by ncdump of earlier versions is those ncdump programs (and most likely other NetCDF utility programs) fail to check null-byte padding. I consider this is not a serious bug in NetCDF library and its utilities, but strictly speaking the file does NOT conform with the CDF specification. FYI, the files generated by NetCDF with non-null byte padding will get NC_ENOTNC from PnetCDF. Because PnetCDF was developed independently from NetCDF, sometimes PnetCDF conforms with the CDF specification more strictly. In fact, as early as the first release of PnetCDF 0.8.0, this null-byte padding checking has been implemented. One possible solution is to develop a new utility program to detect and fix this padding for nc files. CCed to @roblatham00 |
Forgot to mention that there is a PnetCDF utility program named ncmpivalid. It can be used to validate a CDF file and report the exact location that fails to conform with the file specification. |
Given that netCDF hasn't enforced this up to this point, and that apparently enforcing this causes some issues with extant workflows, I am wondering if we need to revert this. I really want to avoid causing disruption to our users, especially when the practical positive impact seems to be limited. It can be reimplemented once we have an easy way for users to fix otherwise valid files, and will be the least disruptive to existing workflows, etc. |
Alternatively this could be made an option instead of removed altogether, and have it turned off for now. I would also add a more specific error message, NC_EHDRPAD or some such, to make it clear what the issue is if the option is turned on. Just thinking out loud. --enable-enforce-null-byte-padding perhaps. |
The problem is the fact NetCDF has partially enforced this null-byte padding from 3.3 release as early as I can tell. For example, null-byte padding is enforced when writing strings, say names of dimension, variables, etc. (see ncx_pad_putn_text() called through v1h_put_NC_string() or ncx_put_NC_string() in earlier versions). Similar padding has been done for attributes of type size less than 4 bytes (e.g. NC_CHAR, NC_BYTE, and NC_SHORT). Same for variables, but in data section, instead of header. PR #332 was created based on the observation of inconsistency in this padding implementation. Specifically, it fixes the only place that fails to enforce null-byte padding, which is when writing the attribute values. |
In a vacuum, I think it's great to enforce this documented standard, but in practice I think we need to accommodate files generated before this was enforced. I'm very hesitant to penalize people, especially without being able to suggest an easy/turn-key way to fix the file. I like the idea of making this a configuration option (for now) that can, when appropriate, be flipped from 'off' by default to 'on'. |
@wkliao may I ask whether there is benefit to enforcing this other than resolving an inconsistency? Also may I ask how these files are created? How are files with incorrect null padding coming into existence? |
I'm going to add this option while managing the other pull requests, but I am curious as well @wkliao . I assume the answer is 'via libnetcdf', in which case I think the burden is definitely on us to not penalize people (as we apparently are now, oops) without warning. I'll also add a test using the file linked above which illustrate the issue. It will be fixed in the 4.5.1 release which I'm hoping to get out as a bugfix release very quickly. |
If these files were generated by libnetcdf, then I agree with @WardF that there is a moral obligation to continue reading them. Even if the files were generated elsewhere, it would be sad to stop reading files that previously could be read. (We have been discussing the utility of logging - this would be a great place to log a warning but not return an error.) However, my concern is also that we understand, fix, and test the bug which caused invalid files to be produced, if they were produced by libnetcdf. In other words, have we stopped the problem from re-occurring? Or does it still happen? |
@edhartnett #332 appears to also correct behavior when writing netcdf files, and I infer that means the files were generated by libnetcdf. I don't intend to gate the writing fix behind an option, only whether or not files should return an error when read or preserve the extant behavior. Given the emphasis on netCDF as an archival format, I want to fix this ASAP. It does make me wonder how much archived data there is out there that would fail when a null byte padded buffer is enforced. That then begs the following question: is there a situation where, even in the presence of a utility to 'fix' the file, it is ok for archival data to default to returning an error when it is read? Adding the option is going to be a great way for people to check their data archives for files that don't adhere to the format, and then potentially correct them to adhere to the format. I suspect this option may never default to 'on', however, for the reason I listed above. I don't want archives to suddenly break and require post-processing to work again. |
Agree wholeheartedly. Existing archives need to be readable. Since pnetcdf rejects these files, I suspect we would be hearing a lot more if these files were very common. However, this may explain one of the reasons that the PIO library is written to first try the user-specified format (usually pnetcdf), and the fall back to using classic netcdf if that didn't work. So for the CESM people, this might be happening frequently, but they don't know. |
I hate to see files generated by NetCDF got rejected by PnetCDF. I believe this is a perfect example of why two independent implementations of the same standard is necessary, like in in MPI world, MPICH vs. OpenMPI. I guess those files in question were created by NetCDF prior to 4.5.0, as I am not aware of other software that creates CDF files without NetCDF or PnetCDF. @WardF, I hate to say this but your configure-time option will still not solve the problem of PnetCDF being unable to read the problematic files. Note this applies to all PnetCDF releases. I would guess the reverse may also exist, waiting for us to discover. At that time, what guideline should be use to resolve the conflict?. This is why, in my personal opinion, abiding to the standard is important. On the other hand, I also hate to see those archive files rejected by the newer releases of NetCDF. I am not insisting the padding checking, but would like to point out the dilemma. I hope a perfect solution can come out from our discussion. At this moment, for automatic solutions, I do not see a better option than your approach, particularly in NetCDF realm. For non-automatic solutions, I propose in my earlier post a new utility program to fix the padding of existing files, still not an elegant one. |
FYI. I will change PnetCDF to skip the padding check for reading and the change will appear in 1.9.0 release. But, I will keep the utility, ncmpivalid intact, which can be useful to report this padding issue. (Wish I can change this behavior for all previous releases.) |
I agree it is a dilemma, and if we could go back and ensure the standard were adhered to from the start, that would be best. At this point, however, rejecting archived files is pretty much a non-starter. I would prefer to find a better way and will continue to think about it. |
To give a little more context, these files were created by our test suite using xarray with the scipy backend with versions:
When we try to read them using the netCDF4 backend we get the invalid argument error. A minimal working example is: In [1]: import xarray as xr
In [2]: da = xr.DataArray([1., 2., 3.], dims=['x'])
In [3]: da.to_netcdf('test_scipy.nc', engine='scipy')
In [4]: ds = xr.open_dataset('test_scipy.nc', engine='netcdf4')
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-4-59fb1fb64f63> in <module>()
----> 1 ds = xr.open_dataset('test_scipy.nc')
//anaconda/envs/research/lib/python2.7/site-packages/xarray/backends/api.pyc in open_dataset(filename_or_obj, group, decode_cf, mask_and_scale, decode_times, autoclose, concat_characters, decode_coords, engine, chunks, lock, cache, drop_variables)
280 if engine == 'netcdf4':
281 store = backends.NetCDF4DataStore(filename_or_obj, group=group,
--> 282 autoclose=autoclose)
283 elif engine == 'scipy':
284 store = backends.ScipyDataStore(filename_or_obj,
//anaconda/envs/research/lib/python2.7/site-packages/xarray/backends/netCDF4_.pyc in __init__(self, filename, mode, format, group, writer, clobber, diskless, persist, autoclose)
208 diskless=diskless, persist=persist,
209 format=format)
--> 210 self.ds = opener()
211 self._autoclose = autoclose
212 self._isopen = True
//anaconda/envs/research/lib/python2.7/site-packages/xarray/backends/netCDF4_.pyc in _open_netcdf4_group(filename, mode, group, **kwargs)
183 import netCDF4 as nc4
184
--> 185 ds = nc4.Dataset(filename, mode=mode, **kwargs)
186
187 with close_on_error(ds):
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__init__()
netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()
IOError: [Errno -36] NetCDF: Invalid argument: '/Users/skc/test_scipy.nc' We experience no issues when using the netCDF4 backend to save the files. Please let me know if you need any more information. |
So the scipy backend is an independent implementation of netCDF3. That code has been around for quite awhile, so while it may be out of spec, there are probably quite a few files in the wild that have been created using it. |
@dopplershift, you are right. I can see an implementation of NetCDF3 in https://github.com/scipy/scipy/blob/master/scipy/io/netcdf.py |
FYI, netcdf has two. There is also a pure java implementation inside netcdf-Java. |
I do not understand this comment:
Is the issue that Ward;s proposed fix can of necessity only apply to the netcdf-c library and not pnetcdf |
It looks like the SciPy implementation of netCDF3 has a (longstanding) bug: it pads with the ASCII character I'm happy to submit a PR to fix SciPy, but I agree that there are likely quite a few archived netCDF3 files out there in the world that were written with SciPy, so it would be great if this check can be relaxed. |
Yeah, should probably fix SciPy, since that's just silly. |
Hi, @DennisHeimbigner, Glad to know more independent implementations of CDF formats. |
I just now took a closer look at the locations in the problematic file that violate the null-byte padding. I found two places. One is the name of global attribute "coordinates" and the other is the attribute values of "coordinates". If this file was generated by NetCDF-C library, then the first violation should never occur. Null-byte padding was enforced for name objects by NetCDF at all time (at least since 3.6.3). Therefore, if Scipy was used to generate the file, as described by @spencerkclark, then SciPy may want to fix this as well. Hi, @shoyer, the location you pointed out is for data section (i.e. when writing variables) not header section. The padding for variables has a different requirement by CDF specifications. It says "padding uses variable's fill value". I guess neither NetCDF-C or PnetCDF enforces this. Currently, both NetCDF-C and PnetCDF uses null-byte padding for variables. |
@wkliao True, but it also uses |
I confirm. |
@shoyer, FYI, variable names are stored in the file header section, not data section. So the null-byte padding applies to variable names. |
I understand the fix I propose will work for NetCDF but not pnetcdf; I am open to a better solution that does not require any modification of existing/archived data. |
Per the netCDF spec, variable names and attributes should be padded with null padding to the nearest 4-byte boundary, but scipy has been padding with b'0' instead: http://www.unidata.ucar.edu/software/netcdf/docs/file_format_specifications.html This surfaced recently after the netCDF-C library added more stringent checks in version 4.5.0 which rendered it unable to read files written with SciPy: Unidata/netcdf-c#657 This change to netCDF-C is likely going to be rolled back, but it's still a good idea for us to write compliant files. Apparently there are other netCDF implementations that also insist on padding with nulls.
Postel's law "Be conservative in what you do, be liberal in what you accept from others" seems like good advice here: create files as closely adhering to the standard as possible, but if one or two implementations were a bit sloppy "invalid file" is harsh -- if there's any way the library can carry on with a best guess, do that. Sounds like that's the resolution here.
|
Hi @WardF, since this issue has been closed by #675, I assume you have a good solution. I can see the configure-time option as you mentioned earlier. Do you mind letting us know a summary of your fix? In PnetCDF, I simply skip the checking for null-byte padding at file open for reading and this will appear in the next release. I also improved a PnetCDF utility program named ncvalidator that can be used to detect the locations of a classic CDF files that fail to conform the file specification, including this null-byte padding. To test ncvalidator, several "bad" classic files have been created artificially in the same folder for testing various header format errors. The file names should be able to describe the kinds of expected errors. For example, file bad_padding_name_dim.nc1, a CDF-1 file, violates null-byte padding at the dimension name. You are welcomed to use them to test NetCDF or SciPy. A few more "bad" files are also available in here created for testing other kinds of header errors. Utility program ncvalidator also can repair the null-byte padding for the legacy files, if the command-line option "-x" is used and the file has write permission. Feedbacks are welcomed. |
@wkliao My fix is essentially what you described; unless the option is specified at configure time, the null-byte padding check is skipped when opening the file to read. |
This will also be fixed in the next release of SciPy: scipy/scipy#8170 |
Upstream changes: ## 4.6.1 - March 15, 2018 * [Bug Fix] Corrected an issue which could result in a dap4 failure. See [Github #888](Unidata/netcdf-c#888) for more information. * [Bug Fix][Enhancement] Allow `nccopy` to control output filter suppresion. See [Github #894](Unidata/netcdf-c#894) for more information. * [Enhancement] Reverted some new behaviors that, while in line with the netCDF specification, broke existing workflows. See [Github #843](Unidata/netcdf-c#843) for more information. * [Bug Fix] Improved support for CRT builds with Visual Studio, improves zlib detection in hdf5 library. See [Github #853](Unidata/netcdf-c#853) for more information. * [Enhancement][Internal] Moved HDF4 into a distinct dispatch layer. See [Github #849](Unidata/netcdf-c#849) for more information. ## 4.6.0 - January 24, 2018 * [Enhancement] Full support for using HDF5 dynamic filters, both for reading and writing. See the file docs/filters.md. * [Enhancement] Added an option to enable strict null-byte padding for headers; this padding was specified in the spec but was not enforced. Enabling this option will allow you to check your files, as it will return an E_NULLPAD error. It is possible for these files to have been written by older versions of libnetcdf. There is no effective problem caused by this lack of null padding, so enabling these options is informational only. The options for `configure` and `cmake` are `--enable-strict-null-byte-header-padding` and `-DENABLE_STRICT_NULL_BYTE_HEADER_PADDING`, respectively. See [Github #657](Unidata/netcdf-c#657) for more information. * [Enhancement] Reverted behavior/handling of out-of-range attribute values to pre-4.5.0 default. See [Github #512](Unidata/netcdf-c#512) for more information. * [Bug] Fixed error in tst_parallel2.c. See [Github #545](Unidata/netcdf-c#545) for more information. * [Bug] Fixed handling of corrupt files + proper offset handling for hdf5 files. See [Github #552](Unidata/netcdf-c#552) for more information. * [Bug] Corrected a memory overflow in `tst_h_dimscales`, see [Github #511](Unidata/netcdf-c#511), [Github #505](Unidata/netcdf-c#505), [Github #363](Unidata/netcdf-c#363) and [Github #244](Unidata/netcdf-c#244) for more information. ## 4.5.0 - October 20, 2017 * Corrected an issue which could potential result in a hang while using parallel file I/O. See [Github #449](Unidata/netcdf-c#449) for more information. * Addressed an issue with `ncdump` not properly handling dates on a 366 day calendar. See [GitHub #359](Unidata/netcdf-c#359) for more information. ### 4.5.0-rc3 - September 29, 2017 * [Update] Due to ongoing issues, native CDF5 support has been disabled by **default**. You can use the options mentioned below (`--enable-cdf5` or `-DENABLE_CDF5=TRUE` for `configure` or `cmake`, respectively). Just be aware that for the time being, Reading/Writing CDF5 files on 32-bit platforms may result in unexpected behavior when using extremely large variables. For 32-bit platforms it is best to continue using `NC_FORMAT_64BIT_OFFSET`. * [Bug] Corrected an issue where older versions of curl might fail. See [GitHub #487](Unidata/netcdf-c#487) for more information. * [Enhancement] Added options to enable/disable `CDF5` support at configure time for autotools and cmake-based builds. The options are `--enable/disable-cdf5` and `ENABLE_CDF5`, respectively. See [Github #484](Unidata/netcdf-c#484) for more information. * [Bug Fix] Corrected an issue when subsetting a netcdf3 file via `nccopy -v/-V`. See [Github #425](Unidata/netcdf-c#425) and [Github #463](Unidata/netcdf-c#463) for more information. * [Bug Fix] Corrected `--has-dap` and `--has-dap4` output for cmake-based builds. See [GitHub #473](Unidata/netcdf-c#473) for more information. * [Bug Fix] Corrected an issue where `NC_64BIT_DATA` files were being read incorrectly by ncdump, despite the data having been written correctly. See [GitHub #457](Unidata/netcdf-c#457) for more information. * [Bug Fix] Corrected a potential stack buffer overflow. See [GitHub #450](Unidata/netcdf-c#450) for more information. ### 4.5.0-rc2 - August 7, 2017 * [Bug Fix] Addressed an issue with how cmake was implementing large file support on 32-bit systems. See [GitHub #385](Unidata/netcdf-c#385) for more information. * [Bug Fix] Addressed an issue where ncgen would not respect keyword case. See [GitHub #310](Unidata/netcdf-c#310) for more information. ### 4.5.0-rc1 - June 5, 2017 * [Enhancement] DAP4 is now included. Since dap2 is the default for urls, dap4 must be specified by (1) using "dap4:" as the url protocol, or (2) appending "#protocol=dap4" to the end of the url, or (3) appending "#dap4" to the end of the url Note that dap4 is enabled by default but remote-testing is disbled until the testserver situation is resolved. * [Enhancement] The remote testing server can now be specified with the `--with-testserver` option to ./configure. * [Enhancement] Modified netCDF4 to use ASCII for NC_CHAR. See [Github Pull request #316](Unidata/netcdf-c#316) for more information. * [Bug Fix] Corrected an error with how dimsizes might be read. See [Github #410](Unidata/netcdf-c#410) for more information. * [Bug Fix] Corrected an issue where 'make check' would fail if 'make' or 'make all' had not run first. See [Github #339](Unidata/netcdf-c#339) for more information. * [Bug Fix] Corrected an issue on Windows with Large file tests. See [Github #385](Unidata/netcdf-c#385]) for more information. * [Bug Fix] Corrected an issue with diskless file access, see [Pull Request #400](Unidata/netcdf-c#400) and [Pull Request #403](Unidata/netcdf-c#403) for more information. * [Upgrade] The bash based test scripts have been upgraded to use a common test_common.sh include file that isolates build specific information. * [Upgrade] The bash based test scripts have been upgraded to use a common test_common.sh include file that isolates build specific information. * [Refactor] the oc2 library is no longer independent of the main netcdf-c library. For example, it now uses ncuri, nclist, and ncbytes instead of its homegrown equivalents. * [Bug Fix] `NC_EGLOBAL` is now properly returned when attempting to set a global `_FillValue` attribute. See [GitHub #388](Unidata/netcdf-c#388) and [GitHub #389](Unidata/netcdf-c#389) for more information. * [Bug Fix] Corrected an issue where data loss would occur when `_FillValue` was mistakenly allowed to be redefined. See [Github #390](Unidata/netcdf-c#390), [GitHub #387](Unidata/netcdf-c#387) for more information. * [Upgrade][Bug] Corrected an issue regarding how "orphaned" DAS attributes were handled. See [GitHub #376](Unidata/netcdf-c#376) for more information. * [Upgrade] Update utf8proc.[ch] to use the version now maintained by the Julia Language project (https://github.com/JuliaLang/utf8proc/blob/master/LICENSE.md). * [Bug] Addressed conversion problem with Windows sscanf. This primarily affected some OPeNDAP URLs on Windows. See [GitHub #365](Unidata/netcdf-c#365) and [GitHub #366](Unidata/netcdf-c#366) for more information. * [Enhancement] Added support for HDF5 collective metadata operations when available. Patch submitted by Greg Sjaardema, see [Pull request #335](Unidata/netcdf-c#335) for more information. * [Bug] Addressed a potential type punning issue. See [GitHub #351](Unidata/netcdf-c#351) for more information. * [Bug] Addressed an issue where netCDF wouldn't build on Windows systems using MSVC 2012. See [GitHub #304](Unidata/netcdf-c#304) for more information. * [Bug] Fixed an issue related to potential type punning, see [GitHub #344](Unidata/netcdf-c#344) for more information. * [Enhancement] Incorporated an enhancement provided by Greg Sjaardema, which may improve read/write times for some complex files. Basically, linked lists were replaced in some locations where it was safe to use an array/table. See [Pull request #328](Unidata/netcdf-c#328) for more information.
Per the recommendation of @dopplershift (cc: @spencerahill), I am migrating an issue (Unidata/netcdf4-python#742) initially posted in netcdf4-python over to here.
Environment Information
libnetcdf
was installed from the conda-forge channel (e.g.conda install -c conda-forge libnetcdf
).Summary of Issue
Since
libnetcdf
version 4.5.0 appeared on conda-forge, we have encountered some puzzling errors in our CI test suite. Namely, some files that we used to be able to open with xarray using netCDF4 now lead to errors like the following:In doing some investigation offline, it seems that the command line tool
ncdump
provided bylibnetcdf
version 4.5.0 is also unable to open these files. E.g. I get the following response:However, using
libnetcdf
version 4.4.1.1,ncdump
is able to handle the file (and using version 4.4.1.1 also enables downstream tools like netcdf4-python to work):I have posted a link to this file as an example. It would be helpful to know if you can reproduce the issues we've encountered here with files like the one provided, and what about them led to this problem. Thanks in advance for your help!
Steps to reproduce the behavior
Using the latest version of
libnetcdf
from the conda-forge channel, download the linked file and try to runncdump
.The text was updated successfully, but these errors were encountered: